我更习惯C而不是Python。作为业余时间的事情,我决定用Python制作一个简单的hangman控制台应用程序。玩家1输入一个单词,然后玩家2有5次尝试猜测。说“你好”一旦他们猜出来,就说“d”它会打印出来。如果他们的猜测是“e”,它会打印出“e”。你知道吗
出于某种原因,不管我猜的是正确的还是错误的字母,它只显示一个_
word = input("Player 1, please enter a word: ")
lives = 5
print("Player 2, you have {} lives left.".format(lives))
print("The Word: ", "_ " * len(word))
wordSuccessfullyGuessed = False
while lives > 0 or wordGuessed:
guess = input("Player 2, guess a letter: ")
wordFormatted = ""
for char in word:
if char in guess:
wordFormatted = char + " "
else:
wordFormatted = "_ "
print(wordFormatted)
这里
输出
输出:
在内部
for
循环中,每次迭代都要重新分配变量wordFormatted
。看起来您想将字母或下划线附加到字符串中。请尝试:似乎还需要在while循环的每次迭代中重新分配
wordFormatted = ""
。这将以每一次猜测来澄清这个词。可能也想看看。你知道吗相关问题 更多 >
编程相关推荐