需要帮忙。 python整数和字符串

时间:2014-10-03 16:31:08

标签: python string integer

我在制作的程序中遇到了一些代码问题。当我运行该文件时,它会显示以下消息:

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 个答案:

答案 0 :(得分:0)

您需要将int转换为str,以便连接。

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)
相关问题