当用户输入“是”时如何让游戏重启?

时间:2017-02-11 00:23:26

标签: python

当用户键入“是”

时,如何重新启动它
import random



answer=(random.randint (1,100))
play_again="yes"

tries=(0)``

guess=int(input("I'm thinking of a number between 1 and 100 "))

tries+=1

while play_again=="yes":

if (guess<answer):
    guess=int(input("its higher than "+str(guess)+" "))
    tries+=1
elif (guess>answer):
    guess=int(input("it's lower than "+str(guess)+" "))
    tries+=1
elif (guess==answer):
        print("well done! You guessed the number in "+str(tries)+" guesses!")
        play_again=input("would you like to play again?")

如果用户在输入“是”时获胜,如何让游戏重启?

3 个答案:

答案 0 :(得分:0)

使用两个循环。一个外部带有play_again == "yes",另一个内部带有guess != answer

while play_again == "yes":
    # get input
    while guess != answer:
        # if lower:
        #     get guess
        # if higher
        #     get guess
    # get play_again

答案 1 :(得分:0)

你可以这样做,创建一个功能!

import random
import sys 

def main():

    # your code here

while True:
   play_again = input("play again? :") # For python 2 raw_input()
   if play_again == "yes":
         main()
   else:
        sys.exit()

这样的事情!

我希望这可以帮助你!!

答案 2 :(得分:0)

使用自我调用的函数

/usr/local/lib