我想指出你的代码有一些问题。
最主要的一点是,当你输入yes时,游戏不会再次运行。它所要做的就是运行main(),它将打印your game,然后询问您是否要重试一次。如果你把你的游戏放在一个定义里,你可以在必要的时候调用它。
而且,我不知道是不是只有我一个人,但如果你猜对了数字,它仍然会让你猜一个数字。您需要通过将play_again()方法放入else块来退出循环。
下面是代码。为了优化,我把它稍微修饰了一下。在import random #imports random number function
def main():
print("Welcome to the number guessing game!")
game = "your game"
print(game)
run_game()
play_again()
def run_game():
counter = 1
number = random.randint(1, 10)
while counter > 0 and counter <= 5:
guess=int(input("Try and guess the number\n"))#tells the user to try and guess the number
if guess!=number and guess > number:
print("wrong number! Try again you are too high")#tells you that you were wrong and that that you were