你需要一个外层
while
循环一次又一次地玩游戏
import os
import random
import time
play = "yes"
while play == "yes":
n = random.randint(1, 10)
g = 0
time.sleep(.25)
print("guess the number 1-10")
time.sleep(.5)
while g != n:
g = int(input("what is your guess ?"))
if g < n:
print("too low guess again")
elif g > n:
print(" too high guess again")
else:
print("you got the number")
play = input("play again? ('yes' to continue) ")
time.sleep(.5)
os.system("clear")
如果您使用
function
def one_run():
n = random.randint(1, 10)
g = 0
print("guess the number 1-10")
while g != n:
g = int(input("what is your guess ?"))
if g < n:
print("too low guess again")
elif g > n:
print(" too high guess again")
else:
print("you got the number")
play = "yes"
while play == "yes":
one_run()
play = input("play again? ('yes' to continue) ")
time.sleep(.5)
os.system("clear")