gpt4 book ai didi

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'str' Simpleton

转载 作者:行者123 更新时间:2023-11-28 20:46:33 25 4
gpt4 key购买 nike

这非常简单,到目前为止我发现的消息都没有处理我的低级问题,但这是我的代码。

name=input("What is your name?")
weight=input("How much do you weigh?")
target=input("What would you like to weigh?")
loss=weight-target
print("So...",name,'You need to lose',loss,"pounds!")
print("\n\nPress enter key to scram!")

错误:

Traceback (most recent call last):
File "/Users/davebrown/Desktop/Pract.py", line 7, in <module>
loss=weight-target
TypeError: unsupported operand type(s) for -: 'str' and 'str'

我不明白为什么它不受支持?

最佳答案

您不能减去 str 对象。 (与 Python 2.x 中的 input 不同,Python 3.x 中的 input 返回 str 对象)

>>> '2' - '1'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'str' and 'str'

将它们转换成intfloat首先。

>>> int('2') - int('1')
1

关于python - 类型错误 : unsupported operand type(s) for -: 'str' and 'str' Simpleton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595125/

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