在python问题的计算器

AnA*_*ner 2 python python-3.x

我在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时,我做错了什么,顺便说一句,我不认为我是一个新手编程的菜鸟

Arp*_*pit 7

input()返回字符串而非数字.这就是为什么不进行添加,而是执行字符串连接.

你需要使用int(x)int(y)转换.

使用此声明 answear = int(x) + int(y)

  • @suhail仅在python 2中,在python 3中它返回一个字符串. (3认同)

Bur*_*lid 6

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)