2017-04-18 78 views
0

程序將不會再運行y輸入,請幫助,n工作正常,不知道爲什麼,是我的while循環?,有這個問題約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() 
+6

這不是一個循環內。我不希望它再次運行。你需要再次調用'main',或者把它換成另一個循環。 – Carcigenicate

回答

0

正如@Carcigenicate說,你會希望把它放在一個循環/功能或致電主再次

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()