gpt4 book ai didi

python - 猜猜字母程序不起作用?

转载 作者:行者123 更新时间:2023-11-30 22:16:31 24 4
gpt4 key购买 nike

我正在创建一个非常基本的“猜字母”游戏。我的代码如下。当我运行该程序并输入正确列出的字母之一时,它仍然运行代码,就好像它是一个不正确的字母一样。我需要修复什么?

letters = ("M","K","O","W","X","S","A")

guess = input("Guess a letter: ")

while guess.lower() != letters:
print("Incorrect!")
input("Guess a letter: ")
if guess.lower() == letters:
print(guess,"is correct!")

input("Press Enter to Continue")

当代码正常工作时,它应该显示如下内容:

Guess a letter: p
Incorrect!
Guess a letter: q
Incorrect!
Guess a letter: m
M is correct!

最佳答案

您需要使用 guess.upper() 而不是 guess.lower() 将用户的猜测与 letters 中的大写字母进行比较。您还应该使用 in 比较猜测的字母,而不是检查与元组是否相等。最后,您需要在 while 语句中再次存储用户更新的猜测:

letters = ("M","K","O","W","X","S","A")

guess = input("Guess a letter: ")

while guess.upper() not in letters:
print("Incorrect!")
guess = input("Guess a letter: ")

print(guess,"is correct!")
input("Press Enter to Continue")

关于python - 猜猜字母程序不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49949874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com