游戏的第一轮工作正常,但是一旦您回答“ 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
答案 0 :(得分:2)
尝试输入Y
而不是y
。您只检查大写字母,如果输入既不是Y
也不是N
,则继续运行一个无限循环。
答案 1 :(得分:1)
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")
将输入更改为小写,然后与小写进行比较。现在您将不会遇到案例问题。