Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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/python-3.x/15.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
Python 通过用户输入,加法程序的正确代码行是什么?_Python_Python 3.x - Fatal编程技术网

Python 通过用户输入,加法程序的正确代码行是什么?

Python 通过用户输入,加法程序的正确代码行是什么?,python,python-3.x,Python,Python 3.x,今天我在64位PC上下载了我的新python 3.8.5版本32位,并尝试了一个简单的添加程序。 这是我的代码: value = input("Please enter a string:\n") value2 = input("Please enter a string:\n") value3 = value+value2 print(f'You entered {value+value2}') print(f'You entered {value3}'

今天我在64位PC上下载了我的新python 3.8.5版本32位,并尝试了一个简单的添加程序。 这是我的代码:

value = input("Please enter a string:\n")
value2 = input("Please enter a string:\n")
value3 = value+value2
print(f'You entered {value+value2}')
print(f'You entered {value3}')
结果是:

= RESTART: C:\Users\mcheg\AppData\Local\Programs\Python\Python38-32\trial_later delete filie1.py
Please enter a string:
12
Please enter a string:
34
You entered 1234
You entered 1234

您需要将输入转换为
int
。默认情况下,输入值为字符串

value = int(input("Please enter a string:\n"))
value2 = int(input("Please enter a string:\n"))
value3 = value+value2
print(f'You entered {value+value2}')
print(f'You entered {value3}')

由于默认情况下输入为字符串,因此如果需要整数,则需要按照以下步骤进行转换:

value1 = int(input("Please enter a string:\n"))
value2 = int(input("Please enter a string:\n"))
我的意思是,您输入了两个字符串(您知道它们是字符串!),然后将它们与
value+value2
连接起来,得到另一个字符串。这是应该发生的