Python是獲取用戶輸入的一個非常基本的疑問,Python是否將任何輸入作爲字符串並將其用於計算,我們必須將其更改爲整數或什麼?在下面的代碼:來自用戶的輸入
a = raw_input("Enter the first no:")
b = raw_input("Enter the second no:")
c = a + b
d = a - b
p = a * b
print "sum =", c
print "difference = ", d
print "product = ", p
Python中給出以下錯誤:
Enter the first no:2
Enter the second no:4
Traceback (most recent call last):
File "C:\Python27\CTE Python Practise\SumDiffProduct.py", line 7, in <module>
d=a-b
TypeError: unsupported operand type(s) for -: 'str' and 'str'
誰能告訴請爲什麼我收到此錯誤?
用戶輸入是字符串。在執行操作之前使用int()。 – Sudipta