簡體   English   中英

程序將不再運行

[英]program won't run again

程序將不會在y輸入后再次運行,請幫助,n正常工作,不知道為什么,這是我的while循環嗎?在大約2個小時的時間里遇到了很多麻煩,我們不勝感激。 我還需要使用True 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() 

正如@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() 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM