循环问题在单词中找到某个字母

2024-04-24 23:52:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个程序,在这个程序中,计算机从一个预定义的列表中选择一个单词,然后用户逐个输入字母来尝试猜测这个单词。你知道吗

我正在尝试循环程序,这样用户就可以不断猜测单词中的字母数,而不管他们猜测是否正确。你知道吗

然而,由于某些原因,如果他们猜对了,程序目前只循环两次,如果他们猜错了,则根本不会循环。我做错什么了?你知道吗

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."))

Tags: the用户in程序youinputishave
2条回答

您需要做的是请求用户在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."
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%确定你在追求什么,但这段代码循环游戏,直到用户输入一个不正确的字母

相关问题 更多 >