2016-11-13 11 views
0

を「暗黙的をstrには 'int型のオブジェクトを変換できません」、私は第1章エラーは、私はちょうど<a href="https://automatetheboringstuff.com/" rel="nofollow noreferrer">Automate The Boring Stuff</a>を始め

myname = input() 
print ('It is nice to meet you,' + myname) 
lengthofname = len(myname) 
print ('your name is this many letters:' + lengthofname) 

でよ、私はこれを実行した、それは私にCan't convert 'int' object to str implicitlyを与えました。 3行目の私の推論は、変数mynameを整数に変換してから4行目に接続したいということです。

なぜこれが誤ったコーディング方法ですか?

+2

http://stackoverflow.com/questions/13654168/typeerror-cant-convert-int-object-to-str-implicitlyを参照してください。あなたが投稿する前にGoogleをしてください。 –

+0

'print()'の引数をカンマで区切り、 'print()'は自動的に文字列に変換します。 'print( 'あなたの名前は多くの文字です:'、lengthofname)' – furas

答えて

0

あなたのコードはPython 3.xのようです。修正されたコードは次のとおりです。 printの間にlengthofnameを文字列に変換するだけです。あなたはprint ('your name is this many letters:' + lengthofname)を持っている場合

myname = input() 
print ('It is nice to meet you,' + myname) 
lengthofname = len(myname) 
print ('your name is this many letters:' + str(lengthofname)) 
2

、Pythonは(もちろん不可能である)文字列に整数を追加しようとしています。

この問題を解決する3つの方法があります。

  1. print ('your name is this many letters:' + str(lengthofname))
  2. print ('your name is this many letters: ', lengthofname)
  3. print ('your name is this many letters: {}'.format(lengthofname))
2

+は、2つの数値を追加したり、2つの文字列を連結することができますので、あなたは問題を抱えている - あなたはstring + numberを持っているので、あなたができる前に数値を文字列に変換する必要があります2つの文字列を連結する - string + str(number)

print('your name is this many letters:' + str(lengthofname)) 

しかし、多くの引数をカンマで区切ってprint()を実行することができます。他の関数と同様に、Pythonは自動的に文字列に変換してからprint()を表示します。

print('your name is this many letters:', lengthofname) 

printは、引数の間にスペースを追加します。
(カンマはスペースを追加しますが、印刷します)

関連する問題