gpt4 book ai didi

python - 如何解决这个猜谜游戏循环问题?

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:12 25 4
gpt4 key购买 nike

游戏的第一轮运行良好,但一旦您回答“Y”,计算机的猜测就会保持不变。当您以“N”响应时,它也不会停止循环。还有当我最近开始学习时,如果我在理解解释时遇到困难,请见谅。 :)

from random import randint
comp_num = randint(1,10)
while True:
guess = int(input("Pick a number 1-10 "))
if comp_num == guess:
print(f"You won, it was {comp_num}")
b = input("Do you want to keep playing? Y/N")
if b == "N":
break
elif b == "Y":
comp_num = randint(1,10)
elif guess < comp_num:
print("too low try again")
elif guess > comp_num:
print("too high try again")

Pick a number 1-10 3
You won it was 3
Do you want to keep playing? Y/Ny
Pick a number 1-10 3
You won it was 3 it still remains 3 after the 100th try
Do you want to keep playing? Y/Nn
Pick a number 1-10 it continues to ask for input

最佳答案

尝试输入 Y 而不是 y。您只检查大写字母,如果输入既不是 Y 也不是 N,则继续运行无限循环。

关于python - 如何解决这个猜谜游戏循环问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56959936/

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