我做了一个小猜数游戏,如果你愿意,我已经让它重新启动。但是它通过回到开头并再次询问“myname”来重新启动,我怎样才能让它回到它想到一个数字的位置,并从第一次记住我的名字?
import random
restart = '1'
def main():
guessestaken = 0
print ('Hello, what is your name?')
myname = raw_input()
number = random.randint(1, 50)
print ('Okay ' + myname + ', I am thinking of a number between 1 and 50. You have 5 guesses.')
while guessestaken < 5:
print ('Take a guess.')
guess = input()
guess = int(guess)
guessestaken = guessestaken + 1
if guess < number:
print ('Your guess is too low.')
if guess > number:
print ('Your guess is too high.')
if guess == number:
break
if guess == number:
guessestaken = str(guessestaken)
print ('Good job ' + myname + ', you got the correct answer in ' + guessestaken + ' guesses')
if guess != number:
number = str(number)
print ('Oh dear ' + myname + ', the number I was thinking of was ' + number + '.')
while restart == '1':
main()
restart = raw_input('Would you like to try again? Press 1 for Yes, and 2 for No: ')
此刻它再次询问我的名字。我的意思是,它是功能性的,只是烦人。
谢谢