2017-11-08 12 views
0

私はPythonを初めて使い、基本を学んでいます。あなたは私のためにこれを修正して、どうやって私が間違っていたのか教えてください。 PythonでTypeError:intではなくstrでなければなりません(基本ユーザ)

age = input('Please enter your age:') 
ten = 10 
agePlusTen= age + ten 
print('You will be', agePlusTen, 'in 10 years' 

Traceback (most recent call last): 
    File "C:/Users/Admin/Documents/Python/6.2 fixed.py", line 3, in <module> 
    print('You will be', age + ten, 'in 10 years') 
TypeError: must be str, not int 
+1

'input'は文字列を返します。数学をする前に 'int'を適用してください。 –

+0

これはメガ・ドゥープですが、私は似たような質問をatmと考えることはできません。 –

+0

なぜあなたはそのようなvarを作るのですか? –

答えて

2

input()戻り列。したがって、最初にageintにキャストしてから、変数tenに追加する必要があります。

次のように動作するはずです。また

age = input('Please enter your age:') 
ten = 10 
agePlusTen= int(age) + ten 
print('You will be ' + str(agePlusTen) + ' in 10 years') 

、あなたは文字列のグループを連結した文字列を印刷する際に、それらのいずれかが文字列でない場合str()機能を使用して文字列に変換することを忘れないでください。

+1

ここでいくつかの[ジューシーなドキュメント](https://docs.python.org/3.4/library/functions.html#input) – GiantsLoveDeathMetal

0

Pythonでは、使用時に常に文字列を返します。 入力を使用するときには、変数を格納するために)(で、変数は、以下の示すような整数の書き込みに年齢変数を変換するために、常に文字列型のようになります。
age = input('Please enter your age:') ten = 10 agePlusTen= int(age) + ten print('You will be', agePlusTen, 'in 10 years'),

私は、これは動作するはずだと思います。

関連する問題