我正在尝试编写一个程序,其中计算机从预定义的列表中选择一个单词,然后用户逐个输入字母以尝试猜测该单词。
我正在尝试循环程序,以便用户可以像字母中的字母数一样多次猜测,无论它们是否正确猜测。
但是,出于某种原因,程序目前只有正确猜测才会循环两次,如果猜错了则根本不循环。我做错了什么?
user_input = str(input("Please pick a letter you think is in the word I have chosen."))
for i in (0, len(computer_choice)) #computer_choice is the word the computer has generated
if user_input in WordList:
user_input = str(input("You got one of the letters! Keep going!"))
else:
user_input = str(input("You did not get one of the letters. Please try again. You have " + str(i) + " attempts left."))
答案 0 :(得分:0)
你需要做的是在for循环中询问用户的随机字母猜测,该循环从0到存储在变量'computer choice'中的单词的长度,如下所示:
for i in range(0, len(computer_choice)) #computer_choice is the word the computer has generated
user_input = str(input("Please pick a letter you think is in the word I have chosen."))
if user_input in computer_choice:
print "You got one of the letters! Keep going!"
else:
print "You did not get one of the letters. Please try again. You have " + str(len(computer_choice)-i-1) + " attempts left."
答案 1 :(得分:0)
import random
ChosenWord = random.choice(["WordOne", "WordTwo"])
while True:
user_input = str(input("Enter letter: "))
if user_input.lower() in ChosenWord.lower():
print("Correct!")
else:
print ("You Lost!")
break
不是100%肯定你的目标,但这段代码循环游戏,直到用户输入错误的字母