首页 > 解决方案 > Python - While 循环 - 从列表中检查

问题描述

我现在正在学习python,教程中有猜谜游戏。你必须猜一个词,你有 3 次尝试,直到你失败。现在我想更改您必须猜测的单词是列表的一部分而不是单个值password = ["123456", "qwerty", "password"]而不是password = "123456"

我尝试更改while userInput != password为,while userInput != password[0-2]但只有列表中的第二项给了我正确的信息。你能帮我吗?

password = ["123456", "qwerty", "password"]
userInput = ""
count = 0
limit = 3
out_of_guesses = False

while userInput != password and not(out_of_guesses):
    if count < limit:
        userInput = input("Enter guess:")
        count = count + 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You failed")

else:
    print("Correct!")

标签: pythonpython-3.x

解决方案


推荐阅读