少し数当てゲームを作りましたが、必要に応じて後で再起動できるようにしました。しかし、最初に戻ってもう一度「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: ')
現時点では、再び私の名前を尋ねます。つまり、機能的で、ただ面倒です。
ありがとう