Python3.7 input()连接整数

时间:2018-09-17 19:04:57

标签: python

我有以下python代码。

a = input("Enter first number") # 2
b = input("Enter second number") # 3

c = a+b # 23 instead of 5
print(c) # prints out 23 why?

我正在使用以下命令运行python:

python3.7 filename.py 

不是将两个数字相加,而是将两个数字连接起来,即使我使用python3.7,也给我23而不是5。

我读过的每个答案都表明它求值并返回正确的类型:

https://www.quora.com/What-is-the-difference-between-raw_input-and-input-in-Python#

1 个答案:

答案 0 :(得分:1)

PagedResponse将字符串连接为a+b会返回字符串。您需要进行显式类型转换,才能使用input函数将输入转换为整数。

int()