我正在努力让一个刽子手程序分配工作。因此,这个游戏的重点是首先让玩家选择一个单词然后用户继续输入字母来获得单词。当用户犯6个错误或猜到正确的单词时,游戏结束。所以这就是该计划的目标:
Please enter an integer number (0<=number<10) to choose the word in the list: 1
从输出中我们看到列表中有10个单词,并且用户选择了索引为1的单词,即马。
计算机现在将所选单词的长度打印为用户的提示:
The length of the word is: 5
然后是提示,要求进行第一次猜测:
Please enter the letter you guess: t
作为对猜测的回应,计算机打印出到目前为止匹配的字母,猜测是否正确(字母是在神秘词中找到的),如果猜测不正确,“Hangman”图形是所示。由于在单词“horse”中找不到字母“t”,在这种情况下会显示以下输出:
The letter is not in the word.
Letters matched so far: _____
------------------
这里,第三行显示“Hangman”图形的第一行。当用户犯更多错误时,此图形将“增长”。如果用户到目前为止发生的错误少于6个,则计算机返回以询问下一个猜测。在我们的示例中,到目前为止的错误数是1,因此计算机返回以提示下一个猜测。 假设用户输入的下一个字母是“e”:
Please enter the letter you guess: e
The letter is in the word.
Letters matched so far: ____e
在这种情况下,发现了这封信(这是“马”这个词的最后一个字母)。匹配如第三行所示。这里,“ _ _e”有4个下划线字符,分别对应于“马”的前四个但不匹配的字符,后跟“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!
现在考虑玩家何时猜出正确的字母
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
所以我的问题是我无法弄清楚如何在print语句中的正确位置获取下划线和字母。我该怎么做?我怎么画刽子手?如果您需要更多说明,请询问。感谢。
P.S。不介意无效的东西。这只是检查输入的数字是否有效。现在我假设它是。
答案 0 :(得分:1)
对于下划线,您可以记住哪些字母已经猜到了,可能是一组。
然后你可以做
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).