私は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()
'input'は文字列を返します。もしそれが有効な 'int'かどうかを知りたければ' int'にキャストし、 'ValueError'例外をキャッチしてください。エラーが発生しなければ、それは有効な整数です。 – Goodies
例は次のとおりです。https://repl.it/repls/AshamedGrimyDiplodocus – Goodies