2014-10-03 71 views
-3

我一直在我正在做的程序中的一行代碼有一些問題。當過我運行該文件,它會顯示此消息:需要幫助。 Python整數和字符串

Traceback (most recent call last): 
    File "C:\Users\Main\Google Drive\computerprograming\mathoperationscalc.py", line 9, in <module> 
    print('the sum of ' + x + ' and ' + y + ' is ') 
TypeError: Can't convert 'int' object to str implicitly 

下面是代碼:

print('please input 1st integer') 
x = input() 
x = int(x) 
print('please input 2nd integer') 
y = input() 
y = int(y) 

Sum = x + y 
print('the sum of ' + x + ' and ' + y + ' is ') 
print(Sum) 
+1

(或任何其他650個SO問題,如果你不屑於谷歌的錯誤消息,你會得到。) – jonrsharpe 2014-10-03 16:36:30

回答

0

你需要轉換的intstr你,讓你可以連接。

x = int(input('please input 1st integer')) 
y = int(input('please input 2nd integer')) 

total = x + y 
print('the sum of ' + str(x) + ' and ' + str(y) + ' is ') 
print(Sum) 

或者使用format

'the sum of {} and {} is {}'.format(x,y,total)