gpt4 book ai didi

python - python中的计算器问题

转载 作者:行者123 更新时间:2023-11-28 19:35:34 26 4
gpt4 key购买 nike

我用 Python 制作了一个计算器,但是当我运行它并执行例如 123 和 321 时,我得到的是 123321 而不是 444,我做错了什么?

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)

最佳答案

input() 返回字符串而不是数字。这就是为什么不执行加法,而是执行字符串连接的原因。

需要使用int(x)int(y)进行转换。

使用这个语句answear = int(x) + int(y)

关于python - python中的计算器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18636410/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com