python - 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!")
解决方案
推荐阅读
- gdb - 从在共享对象中崩溃的交叉编译的可执行文件加载核心转储
- c# - 将数组从模型传递到控制器以与控制器中的数组进行比较
- javascript - Javascript没有隐藏最后一张图片上的按钮
- c# - Exchange Server - 无法序列化上下文
- javascript - 根据窗口高度和窗口宽度以及所需的空白空间计算旋转 div 的宽度
- flutter - 后帧回调 - 循环
- r - 在 r shiny 中绘制单选按钮时找不到错误对象
- flask - SQL Alchemy AttributeError:“str”对象没有属性“_sa_instance_state”
- c# - 在多线程应用程序中同步属性值的正确方法
- oracle - ORACLE SQL DEVELOPER-select count(*) from multiple databases