if语句循环,while循环


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")

我试图创建一个循环,并给出用户猜测的数字线索。我首先尝试了if语句,但它当然没有循环,我尝试了多种方法——这是我能想到的最好的方法,但它并不完全起作用——它会一直告诉我,例如:它较小,但当它变大时,它就停止了,程序没有发送任何东西,但如果我正确地得到了数字它会说祝贺,而且我想在用户获胜并键入y后从头开始,但我完全不知道如何进行

按此顺序尝试

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")

最新更新