2017-04-18 11 views
0

プログラムはy入力時に再度実行されません。お手伝いをしてください。nはうまく動作しません。なぜなら、これは私のwhileループですか?助けに感謝します。私はしばらく使用する必要があります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() 
関連する問題