我正在尝试让一个刽子手程序分配工作。所以这个游戏的重点是首先让玩家选择一个单词,然后用户不断输入字母来获取单词。当用户犯 6 个错误或猜对了单词时,游戏结束。所以程序应该是这样的:
Please enter an integer number (0<=number<10) to choose the word in the list: 1
从输出中我们看到列表上有 10 个单词,并且用户选择了索引为 1 的单词,即 horse。
计算机现在打印所选单词的长度作为对用户的提示:
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
在这种情况下,找到了字母(它是单词“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!
现在考虑玩家何时猜对了字母
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
所以我的问题是我无法弄清楚如何在打印语句中的正确位置获取下划线和字母。我该怎么做?还有我怎么画刽子手?如果您需要更多说明,请询问。谢谢。
PS不介意无效的东西。这只是为了检查输入以选择单词的数字是否有效。现在我假设它是。