我正在尝试制作一个随机数猜测游戏,如果用户得到它,我会问他们是否想再玩一次。问题是,如果他们同意,随机数将与上次相同。我是 python 新手,如果你能帮助我,我真的很感激。另外,如果有什么可以改进的地方,请告诉我!
这是我的代码:
while True:
i = 1
import random
number = random.randint(1, 1000)
while 1 != 0:
player_guess = input("Choose a number from 1-1000 ")
if int(player_guess) == number:
print("You got it!")
i-1
x = input("do you want to play again? (y/n) ")
if x == "y":
import random
continue
if x == "n":
exit()
elif int(player_guess) >= number:
print("Too high!")
elif int(player_guess) <= number:
print("Too low!")
你必须存在内循环
while 1 != 0
- 它需要break
而不是continue
。
命令
continue
转到当前 while 1 != 0
的开头,但不会退出。
import random # PEP8: all imports at the beginning of code
while True:
number = random.randint(1, 1000)
while True:
player_guess = input("Choose a number from 1-1000 ")
player_guess = int(player_guess)
if player_guess == number:
print("You got it!")
x = input("do you want to play again? (y/n) ")
x = x.lower()
if x == "y":
break # exit current loop
elif x == "n":
exit()
elif player_guess >= number:
print("Too high!")
elif player_guess <= number:
print("Too low!")