if 语句循环和 while 循环

时间:2021-04-25 20:15:33

标签: python if-statement while-loop

number = int(input("please choose your number: ")) 

while number > number_to_guess: 
    number = int(input("Your guess is wrong it was bigger then the generated number, try again: "))
while number < number_to_guess : 
     number = int(input("Your guess was wrong it was smaller then the generated number, try again: "))

if number == number_to_guess:
    print("Congrats you won")
    restart = input("Do you want to play again? if yes type y, if not you can close the window \n")

我正在尝试创建一个循环并提供用户必须猜测的数字的线索不完全工作它会一直告诉我例如:它更小但是当它变大时它只是停止并且程序没有发送任何东西但是如果我正确地得到了号码它会说恭喜,我也想成功在用户获胜并输入 y 后从头开始重新启动,但我完全不知道该怎么做

1 个答案:

答案 0 :(得分:0)

按这个顺序试试

number = int(input("please choose your number: "))
number_to_guess = 5
while number != number_to_guess:
    if number > number_to_guess:
        number = int(input("Your guess is wrong it was bigger then the generated number, try again: "))
        continue
    if number < number_to_guess :
        number = int(input("Your guess was wrong it was smaller then the generated number, try again: "))
        continue
print("Congrats you won")
restart = input("Do you want to play again? if yes type y, if not you can close the window \n")