我正在尝试编写以下游戏:
我遇到的问题是,我无法弄清楚如何生成新的随机数,并且当用户猜中该数字或丢失该数字时,将打击次数重置为零。
import random
game_stop = False
strikes = 0
random_list = [1,2,3,4,5,6,7,8,9,]
random_number = random.choice(random_list)
def play_again():
play_again = input("play again?: ")
if play_again == "yes":
strikes = 0
game_stop = False
return random.choice(random_list)
while not game_stop:
strikes = strikes + 1
guess = input("enter guess: ")
if guess == "exit":
break
if int(guess) > random_number:
print("too high")
elif int(guess) < random_number:
print("too low")
else:
print("you guessed it!")
print("It took you " + str(strikes) + " tries!")
play_again()
if strikes > 2:
print("three strikes your out!")
play_again() '''
修改您的play_again()函数,使用raw_input
而不是input
:
def play_again():
play_again = raw_input("play again?: ")
if play_again == "yes":
strikes = 0
game_stop = False
return random.choice(random_list)