结束我的秘密文字游戏并用Python输出用户猜测计数
我试图用Python编写一个类似刽子手的文字游戏。 用户可以猜测的次数没有限制 我的游戏运行正常,除了一个小问题: 当用户猜到完整的单词时,游戏不会结束并告诉他们他们赢了,以及猜了多少次结束我的秘密文字游戏并用Python输出用户猜测计数,python,for-loop,while-loop,Python,For Loop,While Loop,我试图用Python编写一个类似刽子手的文字游戏。 用户可以猜测的次数没有限制 我的游戏运行正常,除了一个小问题: 当用户猜到完整的单词时,游戏不会结束并告诉他们他们赢了,以及猜了多少次 def main(): print("Welcome to the Secret Word Game!") print() word = ["m","o","u","n","t","a","i","n"] guessed = [] wrong = []
def main():
print("Welcome to the Secret Word Game!")
print()
word = ["m","o","u","n","t","a","i","n"]
guessed = []
wrong = []
guesses = 0
while True:
out = ""
for letter in word:
if letter in guessed:
out = out + letter
else:
out = out + "*"
if out == word:
print("You guessed", word)
break
print("Guess a letter from the secret word:", out)
guess = input()
if guess in guessed or guess in wrong:
print("Already guessed", guess)
guesses +=1
elif guess in word:
print("Correct!",guess,"is in the secret word!")
guessed.append(guess)
guesses+=1
else:
print("Wrong!")
wrong.append(guess)
guesses+=1
print()
print(guesses)
main()
您不退出,因为您的终止条件不可能为真:
if out == word:
out
是长度为8的字符串word
是由8个单字符字符串组成的列表。如果将word
的初始化更改为
word = "mountain"
然后,您的程序工作正常,生成逐猜测更新和适当数量的猜测
Welcome to the Secret Word Game!
Guess a letter from the secret word: ********
n
Correct! n is in the secret word!
Guess a letter from the secret word: ***n***n
e
Wrong!
Guess a letter from the secret word: ***n***n
t
Correct! t is in the secret word!
Guess a letter from the secret word: ***nt**n
a
Correct! a is in the secret word!
Guess a letter from the secret word: ***nta*n
o
Correct! o is in the secret word!
Guess a letter from the secret word: *o*nta*n
i
Correct! i is in the secret word!
Guess a letter from the secret word: *o*ntain
f
Wrong!
Guess a letter from the secret word: *o*ntain
m
Correct! m is in the secret word!
Guess a letter from the secret word: mo*ntain
u
Correct! u is in the secret word!
You guessed mountain
9