程序不会再次运行

时间:2017-04-18 17:57:00

标签: python input

程序在y输入后再次运行,请帮助,n工作得很好,不知道为什么,是我的循环?,对此有大量麻烦大约2小时任何帮助表示赞赏。我也需要使用一段时间True:?????

import random

def main():

        print("This program will play a game with you! You'll get 3 chances to guess correctly at a number chosen at random by the computer betwen 1 and 10")
        yn = input("Would you like to play the game?(Y/N): ")
        if yn.upper() != 'Y':
            print("Ok have a nice day!")

        the_number = random.randint(1, 10)
        guess = int(input("Take a guess: "))
        tries = 1

        # guessing loop
        while guess != the_number:
            if guess > the_number:
                print("Got to go lower bud")
            else:
                print("Got to go higher bud")

            guess = int(input("Take a guess: "))
            tries += 1
            if tries == 3:
                print ("You failed to guess in time, the number was", the_number)
                break
            if guess == the_number:
                print("You guessed it! The number was", the_number)
                print("And it only took you", tries, "tries!")

        yn = input("Would you like to play the game?(Y/N): ")
        if yn.upper() != 'Y':
            print("Ok have a nice day!")

if __name__ == "__main__":
        main() 

1 个答案:

答案 0 :(得分:0)

正如@Carcigenicate所说,你想把它放在循环/函数中或再次调用main

import random

def main():
    print("This program will play a game with you! You'll get 3 chances to guess correctly at a number chosen at random by the computer betwen 1 and 10")    
    yn = input("Would you like to play the game?(Y/N): ")
    while yn.upper() == 'Y':
        the_number = random.randint(1, 10)
        guess = int(input("Take a guess: "))
        tries = 1

        # guessing loop
        while guess != the_number:
            if guess > the_number:
                print("Got to go lower bud")
            else:
                print("Got to go higher bud")

            guess = int(input("Take a guess: "))
            tries += 1
            if tries == 3:
                print ("You failed to guess in time, the number was", the_number)
                break
            if guess == the_number:
                print("You guessed it! The number was", the_number)
                print("And it only took you", tries, "tries!")
        yn = input("Would you like to play the game?(Y/N): ")
    print("Ok have a nice day!")

if __name__ == "__main__":
    main()