猜字游戏
要求:
创建一个经典的美国游戏,这是一个猜谜游戏。用户需要通过输入逐个字符来猜测一个单词。
要猜测的词由一排- - - - - - -表示(每一个‘-’代表相应字母的位置)。
如果玩家猜到单词中存在的字母,脚本会以所有正确的位置写入该字母。
玩家有10个回合来猜单词。
您可以通过更改变量轻松自定义游戏。您将使用计数器和循环。示例执行:
Secret Word: - - - - - - -
Enter Letter: a
Output: There is no letter ‘a’ in the secret word. You have 9 tries left
Enter Letter: d
Secret Word: - - d - - - -
等等。
如果用户在最大猜测次数之前猜到了正确的单词,则将显示单词和消息“Congratulations: You guessed the correct WORD”。比如Secret Word: Midterm. Congratulations: You guessed the correct WORD.
如果用户在最大猜测次数之前没有猜到正确的单词,则将显示一条消息。 例如:You have reached your maximum guesses. The secret word was Midterm. You failed to guess the secret word.
请记住,用户最多只能猜测 10 次。
注意:该字母将更换为其所有位置。 例如:如果机密单词是Welcome,并且用户猜到了字母"e"。 字母"e"在单词中出现 2 次,因此在字母的所有匹配项中替换短划线。 例如: - e - - - - e
Secret_word = 'welcome'
SW = list(Secret_word)
Guess_word = ['-'] * len(Secret_word)
print('Secret Word:',end=' ')
for i in range(len(Guess_word)):
print(Guess_word[i],end=' ')
print()
count=0
while True:
Guess_letter = input('Enter Letter:')
if Guess_letter not in Secret_word:
count = count + 1
print('Output: There in no letter %s in the secret word. You have %d tries left'%(Guess_letter,10-count))
else:
print("Secret Word: ")
for j in range (len(Secret_word)):
if Guess_letter == Secret_word[j]:
Guess_word[j] = Guess_letter
for i in range(len(Guess_word)):
print(Guess_word[i],end=' ')
print()
if Guess_word == SW:
print('Sercet Word: '+Secret_word+'. Congratulations: You guessed the correct WORD.')
break
if 10-i == 0:
print('You have reached your maximum guesses. The secret word was '+Secret_word+'. You failed to guess the secret word.')
break
觉得有用请点个赞吧,谢谢.