Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.7 input()连接整数_Python - Fatal编程技术网

Python3.7 input()连接整数

Python3.7 input()连接整数,python,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 不是添加两个数字,而是连接两个数字,并给我23而不是5,即使我使用的是python3.7 我读到的每个答案都表示它计算并返回正确的

我有以下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 
不是添加两个数字,而是连接两个数字,并给我23而不是5,即使我使用的是python3.7

我读到的每个答案都表示它计算并返回正确的类型:


a+b
将字符串串联为
输入
返回字符串。您需要显式类型转换,才能使用
int()
函数将输入转换为整数

a = int(input("Enter first number")) 
b = int(input("Enter second number"))

默认情况下,
input()
始终返回字符串。使用3.7并不重要。
input()
返回字符串,而不是整数。用
红色
朗姆酒
试试。在
input()
调用周围使用
int(…)
将字符串转换为整数。谢谢!看起来web上有很多文章是不正确的,因为它们建议输入计算并返回正确的类型。@johndoe
input()
在Python2中对输入调用
eval()
。那是个糟糕的主意,所以被取消了。如果Python3上的文章表明情况仍然如此,那么就没有一篇文章是正确的——它总是一个字符串。