我在python中制作了一个计算器
import time
print("Calculator 1.0")
print("made by AnAwesomeMiner")
print("Number 1 in calculation")
x = input()
print("Number 2")
y = input()
print("calculating")
time.sleep(3)
print("why is this not done yet")
time.sleep(3)
print("god this is taking forever")
time.sleep(3)
print("done")
answear = x + y
print(answear)
Run Code Online (Sandbox Code Playgroud)
但是当我运行它并做例如123和321我得到123321而不是444时,我做错了什么,顺便说一句,我不认为我是一个新手编程的菜鸟
input()返回字符串而非数字.这就是为什么不进行添加,而是执行字符串连接.
你需要使用int(x)和int(y)转换.
使用此声明 answear = int(x) + int(y)
input 返回一个字符串,当你组合两个字符串时,结果就是你所看到的.
>>> x = '123'
>>> y = '321'
>>> x+y
'123321'
Run Code Online (Sandbox Code Playgroud)
所以你需要将它们转换为整数,如下所示:
answear = int(x) + int(y)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |