用Python3编写hangman程序

用Python3编写hangman程序,python,string,algorithm,Python,String,Algorithm,我想让一个刽子手计划任务开始工作。所以这个游戏的重点是首先让玩家选择一个单词,然后用户继续输入字母来得到这个单词。当用户犯了6个错误或猜对了单词时,游戏结束。这就是这个项目的运作方式: Please enter an integer number (0<=number<10) to choose the word in the list: 1 随后出现提示,询问第一个猜测: Please enter the letter you guess: t 作为对猜测的回应,计算机打印出迄

我想让一个刽子手计划任务开始工作。所以这个游戏的重点是首先让玩家选择一个单词,然后用户继续输入字母来得到这个单词。当用户犯了6个错误或猜对了单词时,游戏结束。这就是这个项目的运作方式:

Please enter an integer number (0<=number<10) to choose the word in the list: 1
随后出现提示,询问第一个猜测:

Please enter the letter you guess: t
作为对猜测的回应,计算机打印出迄今为止匹配的字母,以及猜测是否正确(该字母是在神秘单词中发现的),如果猜测不正确,则显示“刽子手”图形。由于在单词“horse”中找不到字母“t”,因此在这种情况下,显示以下输出:

The letter is not in the word.
Letters matched so far: _____
------------------
这里,第三行显示了“刽子手”图形的第一行。当用户犯更多错误时,此图形将“增长”。如果用户到目前为止犯的错误少于6个,计算机将返回询问下一个猜测。在我们的例子中,到目前为止错误的数量是一个,因此计算机返回提示进行下一次猜测。 假设用户输入的下一个字母是“e”:

在本例中,找到了该字母(它是单词“horse”的最后一个字母)。匹配如第三行所示。在这里,“_e”有4个下划线字符,对应于“horse”的前四个尚未匹配的字符,后面是“e”,这是迄今为止匹配的单个字符。 然后,交互会重复。作为一个例子,考虑的情况下,当用户只做了错误的猜测,并没有发现任何更多的字符“马”。完整的输出结果如下所示:

Please enter the letter you guess: u
The letter is not in the word.
Letters matched so far: ____e
------------
| |

Please enter the letter you guess: a
The letter is not in the word.
Letters matched so far: ____e
------------
| |
| O

Please enter the letter you guess: i
The letter is not in the word.
Letters matched so far: ____e
------------
| |
| O
| / |

Please enter the letter you guess: d
The letter is not in the word.
Letters matched so far: ____e
------------
| |
| O
| / |
| |

Please enter the letter you guess: b
The letter is not in the word.
Letters matched so far: ____e
------------
| |
| O
| / |
| |
| / |
|
|

Too many incorrect guesses. You lost!
The word was: horse.
Goodbye! 

现在考虑玩家猜对正确字母< /P>

Please enter the letter you guess: o
The letter is in the word.
Letters matched so far: _o__e


Please enter the letter you guess: r
The letter is in the word.
Letters matched so far: _or_e


Please enter the letter you guess: h
The letter is in the word.
Letters matched so far: hor_e


Please enter the letter you guess: s
The letter is in the word.
Letters matched so far: horse

You have found the mystery word. You win!
Goodbye!
这就是我到目前为止所做的:

words = ['hello', 'horse', 'bye', 'moose', 'earth']    

#Choosing word
choose=input('Please enter an integer number 0<=number<10 to choose the word: ')
#check validity of guess
notValid=checkValidity(guess)

secret=words[int(choose)]
#print length of word
lenword=len(secret)
print('The length of the word is %i' %lenword)

while notValid==False:
    mistake=0

    while mistake<6:
        guess=input('Please enter the letter you guess: ')
        for letter in secret:
            if guess == letter:
                print('The letter is in the word.')
        else:
            print('The letter is not in the word.')
            mistake+=1
words=['hello','horse','bye','moose','earth']
#选词

choose=input('请为下划线输入一个整数0,您可以记住已经猜到了哪些字母,可能是在一组字母中

那你就可以做了

print(" ".join(letter if letter in found else '_' for letter in word))
如何画刽子手?我会把它放到一个函数中:

def draw_man(level):
    parts=['------------', '| |', '| O', '| / |', '| |', '| / |', '|', '|']
    for line in parts[:level]:
        print(line)
    return level <= len(parts) # This return value indicates if we have reached the limit (lost) or not (yet).
def draw_man(级别):
部分=['-----------'、'| |'、'| O'、'|/|'、'|/|'、'|/|'、'|'、']
对于零件中的线[:标高]:
打印(行)

返回级别如果你会注意到,Python还有很多其他的刽子手问题,你可以从那里开始寻找答案,这个问题毫无疑问是其中一些问题以某种形式的重复。我的建议是看看其他人在做什么来解决这个问题。这个问题做得很好,但你应该在网站上找到答案b在你发布你的问题之前,我相信你会找到一个。是的,我先检查了。有一个这样的问题,但没有回答。b/c问题显然不清楚。所以我想我应该开始一个新问题,更好地解释问题是什么。如果你使用python 2.x,你最好使用
raw\u input()
而不是
input()
,因为
input()
在Python2.x中是邪恶的
print(" ".join(letter if letter in found else '_' for letter in word))
def draw_man(level):
    parts=['------------', '| |', '| O', '| / |', '| |', '| / |', '|', '|']
    for line in parts[:level]:
        print(line)
    return level <= len(parts) # This return value indicates if we have reached the limit (lost) or not (yet).