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

Python 如何解决这个猜谜游戏循环问题?,python,python-3.x,Python,Python 3.x,游戏的第一轮运行良好,但一旦你回答“Y”,计算机的猜测保持不变。当您以“N”响应时,它也不会停止循环。还有,当我最近开始学习“如果我理解解释有困难,请道歉”。) 来自随机导入randint comp_num=randint(1,10) 尽管如此: 猜测=整数(输入(“选择数字1-10”)) 如果comp_num==猜测: 打印(f“您赢了,它是{comp_num}”) b=输入(“您想继续播放吗?是/否”) 如果b==“N”: 打破 elif b==“Y”: comp_num=randint(1

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

来自随机导入randint
comp_num=randint(1,10)
尽管如此:
猜测=整数(输入(“选择数字1-10”))
如果comp_num==猜测:
打印(f“您赢了,它是{comp_num}”)
b=输入(“您想继续播放吗?是/否”)
如果b==“N”:
打破
elif b==“Y”:
comp_num=randint(1,10)
elif guesscomp_num:
打印(“太高,请重试”)
选择一个数字1-10 3
你赢了是3分
你想继续玩吗?Y/纽约
选择一个数字1-10 3
你赢了3分,第100次尝试后仍然是3分
你想继续玩吗?是/否
选择一个数字1-10,它会继续要求输入
来自随机导入randint
comp_num=randint(1,10)
尽管如此:
猜测=整数(输入(“选择数字1-10”))
如果comp_num==猜测:
打印(f“您赢了,它是{comp_num}”)

b=输入(“您想继续播放吗?Y/N”)。请尝试输入
Y
,而不是
Y
。您只检查大写字母,如果输入既不是
Y
也不是
N

请查看输入内容(
Y
)的大小写和检查内容(
“Y”
)。这就是你实际输入它的方式吗?为什么你要输入小写的
y
,并用大写的
y
进行测试?这些是不同的-使用
b=input(“你想继续玩吗?Y/N”)。upper()
将其转换为upper-如果你输入
“asdlfib”
,会发生什么情况-你根本不处理不一致的输入你可以使用
string.lower()
独立于大小写测试等价性这与你的问题无关,但我认为最好将游戏循环放在函数中,这样你就可以
返回
,而不是使用
中断
,我完全忘记了这一点。谢谢你,谢谢你的帮助。
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
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").lower() ## <<<<---- See here
        if b == "n":
            break
        elif b == "y":
            comp_num = randint(1,10)
        else:
            print("Not a valid choice!")
    elif guess < comp_num:
        print("too low try again")
    elif guess > comp_num:
        print("too high try again")