2017-11-14 17 views
1

私はPython 3で作業していましたし、 "数字を推測する"という単純なプロジェクトを作ったので、私はここで問題を抱えていますわかる。私はスクリプトに何かを追加するまでゲームを見つけました。これはguess = input()が数字かどうかをチェックすることです。私はそれを実行すると、それは常に私にエラーメッセージを送信し続けます。 あなたは私のコードをチェックして、guess = input()が数字であるかどうかをチェックする方法を知ることができますか?数字のゲームのpython 3の推測に問題がある

Zelandini

import random 


def main(): 
    USER = input('Hello there wht is your name?') 
    print('Hello, ' + USER + '') 
    question = input('Do you want to play a game? [Yes/No]') 

    if question == 'n' or question == 'No' or question == 'no' or question == 'N' or question == 'NO': 
     print('Bye, ' + USER + '!') 

    if question == 'y' or question == 'Yes' or question == 'Y' or question == 'yes': 
     print("Okay, the game is to guess my number,") 

     replay() 


def replay(): 
    tries = 1 
    number = random.randint(1, 10) 

    play_again = True 

    print("I'm thinking a number between 1 and 10") 
    Guess = input("Have a guess?") 

    while play_again: 

     if Guess > number: 
      print("Go lower, your number is too big") 

     if Guess < number: 
      print("Go higher, you number is to small") 

    while Guess != number: 
     tries += 1 
     Guess = int(input("Try again?")) 

     if tries == 4: 
      print('You have out of tries') 

      print("\nWould you like to play again?") 

      response = input("> ").lower() 
      if response not in ("yes", "y"): 
       play_again = False 
       print("Bye!") 

      replay() 

     if Guess > number: 
      print("Go lower, your number is too big") 

     if Guess < number: 
      print("Go higher, you number is to small") 

    if Guess == number: 
     print("Congratulations! You've got it") 
     print("\nWould you like to play again?") 

    response = input("> ").lower() 
    if response not in ("yes", "y"): 
     play_again = False 
     print("Bye you Sad!") 

    replay() 
if __name__ == "__replay__": 
    replay() 


if __name__ == "__main__": 
    main() 
+0

'input'は文字列を返します。もしそれが有効な 'int'かどうかを知りたければ' int'にキャストし、 'ValueError'例外をキャッチしてください。エラーが発生しなければ、それは有効な整数です。 – Goodies

+0

例は次のとおりです。https://repl.it/repls/AshamedGrimyDiplodocus – Goodies

答えて

0

あなたはこの微調整コード(をスコープ& くぼみ)を実行する場合は、もはやノンストップのエラーメッセージが表示されませんと機能の権利を得ることに集中することができます。私はあなたのために、幸せなデバッグを台無しにしません。 :)

import random 

def main(): 
USER = input('Hello there wht is your name?') 
print('Hello, ' + USER + '') 
question = input('Do you want to play a game? [Yes/No]') 

if question == 'n' or question == 'No' or question == 'no' or question == 'N' or question == 'NO': 
    print('Bye, ' + USER + '!') 


if question == 'y' or question == 'Yes' or question == 'Y' or question == 'yes': 
    print("Okay, the game is to guess my number,") 

replay() 

def replay(): 
tries = 1 
number = random.randint(1, 10) 

play_again = True 

while play_again: 

print("I'm thinking a number between 1 and 10") 
guess = input("Have a guess?") 

while guess != number: 
    tries += 1 
    guess = int(input("Try again?")) 

    if tries == 4: 
     print('You have out of tries') 

     print("\nWould you like to play again?") 

     response = input("> ").lower() 
     if response not in ("yes", "y"): 
      play_again = False 
      print("Bye!") 

     replay() 

    if guess > number: 
     print("Go lower, your number is too big") 

    if guess < number: 
     print("Go higher, you number is to small") 

if guess == number: 
print("Congratulations! You've got it") 
print("\nWould you like to play again?") 

response = input("> ").lower() 
if response not in ("yes", "y"): 
    play_again = False 
    print("Bye you Sad!") 

replay() 

if name == "main": main() 
+0

ユーザー入力が数字かどうかを確認するコードを追加できますか? –

関連する問題