2016-11-03 7 views
-2

プログラミングでは完全なnewbです。私はチュートリアルを見ていて、Pythonをプログラムする方法についての本を読んでいます。だから、私は自分自身で数値ジェネレータを作成したいと思っていますし、いくつかのチュートリアルを見てきましたが、コードを再作成したくありません。基本的には、私が得た情報を自分の推測者にしたいのです。ランダム番号推測者

import random 

# Random Numbergenerator Guesser 

print("Hello and welcome to the random number guesser.") 
print("I am guessing a number of 1 - 20. Can you guess which one?") 

x = random.randint(1,20) 

# Here you guess the number value of 'x' 
for randomNumber in range (1,7): 
    randomGuess = input() 
    if randomGuess > x: 
     print("Too high. Guess again!") 
    elif randomGuess < x: 
     print("Too low. Guess again!") 
    else: 
     break 

# Checks to see if the number you were guessing is correct or takes you to a fail screen. 
if randomGuess == x: 
    print("Correct number!") 
else: 
    print("Too many tries. You have failed. The number I was thinking of was " + (x))`` 

このエラーは引き続き発生します。

C:\Python\Python35\python.exe "C:/Users/Morde/Desktop/Python Projects/LoginDataBase/LoginUserDatabse1File.py" 
Hello and welcome to the random number guesser. 
I am guessing a number of 1 - 20. Can you guess which one? 
1 
Traceback (most recent call last): 
    File "C:/Users/Morde/Desktop/Python  Projects/LoginDataBase/LoginUserDatabse1File.py", line 12, in <module> 
    if randomGuess > x: 
TypeError: unorderable types: str() > int() 
+1

使用 'randomGuess = INT(入力())' 。 –

+0

'input()'は文字列( 'str'オブジェクト)を返します。文字列から' int'オブジェクトを作成する必要があります。 – cdarke

答えて

0

まず、あなたのフォーマットは悪いです。第2に、string(あなたはinput()から取得)とintegerを比較しているため、エラーが発生しています。あなたは使用してintegerに入力値を変換する必要があります。

randomGuess = int(input()) 
+0

私はそれを正確に変更しましたが、今は違うと言って別のエラーが出ています。引数は文字列、バイトのようなオブジェクト、または数字で、 'builtin_function_or_method'ではありません。 –

+0

答えでは、次のように言います: 'int(input()); randomGuess = int(input)() TypeError:int 'input()'の周りに '' ''() 'をチェックしてください' 'のあなたのものと比較して –

+0

@anonymousとまったく同じです。 'int()'関数は入力パラメータ 'input()'をとります。したがって、 'int(input)()'ではなく 'int(input())'です。 – Fejs

0

エラーがこの行である:あなたが文字列randomGuessrandomGuessに整数xを比較しようとするため

if randomGuess > x: print("Too high. Guess again!") 

ますので、文字列です。

randomGuess = input() 

次のように定義してください:

randomGuess = int(input()) 

は整数としてあなたの推測の数を考慮することのpythonを強制する(しかし、あなたはケースを処理する必要がある場合、ユーザ入力整数以外の何か)

+0

私はまだエラーが発生しています。それは今言う。 ファイル: "C:/ Users/Morde/Desktop/Python Projects/LoginDataBase/LoginUserDatabse1File.py"、行9、 randomGuess = int(input)() TypeError:int()引数は文字列でなければならず、バイト「builtin_function_or_method」以外のオブジェクトや数字です –

+0

私の答えやFejsをもう一度読んで – JMat

+0

申し訳ありません、それは私の悪かったです。私はしました:randomGuess = int(input())の代わりに 'randomGuess = int(input)()' –