age = raw_input ('How old are you? ')
print "In two year's time you will be: " , age + 2
如何獲取此代碼的最後一行工作?當我在Python中運行時出現錯誤TypeError: cannot concatenate 'str' and 'int' objects
。如何連接'str'和'int'對象?
age = raw_input ('How old are you? ')
print "In two year's time you will be: " , age + 2
如何獲取此代碼的最後一行工作?當我在Python中運行時出現錯誤TypeError: cannot concatenate 'str' and 'int' objects
。如何連接'str'和'int'對象?
使用要挾int
到str
,並使用str.format將它添加到您的字符串:
age = int(raw_input ('How old are you? '))
print "In two year's time you will be: {}".format(age + 2)
我們可以通過年齡類型轉換成int,然後將它添加到一個int串連它。
age = raw_input ('How old are you? ')
print "In two year's time you will be: " , int(age) + 2
逸岸更好的方法來打印這是使用格式:
print "In two year's time you will be: {}".format(int(age) + 2)
代碼有什麼問題?請提供downvote的解釋。 – blackmamba
哦。對於那個很抱歉。我會牢記這一點。 – blackmamba
age = raw_input ('How old are you? ')
print "In two year's time you will be: " , str(int(age) + 2)
這應該如果你正在使用Python 2.7的工作,不知道這是否會在3.X工作
'age = int(raw_input('你多大了?'))'? –