Python 通过用户输入,加法程序的正确代码行是什么?
今天我在64位PC上下载了我的新python 3.8.5版本32位,并尝试了一个简单的添加程序。 这是我的代码: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}'
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
连接起来,得到另一个字符串。这是应该发生的