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

标签 python python-3.x

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

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/

相关文章:

python - 如何在google colaboratory中上传数据集?

java - 用于 Wifi 状态的 Android Monkeyrunner 插件

python-3.x - Airflow 1.10 : sqlalchemy. orm.exc.NoResultFound:未找到 one() 的行

python - 仅提取首字母大写的整个单词

Python3 看不到 Django

python - 使用逗号时,除了处理程序中的语法无效

python - 如何将 Pandas 数据框列从 np.datetime64 转换为日期时间?

python - Django 查询单下划线表现得像双下划线?

python - 理解 python 中的 lambda 并使用它来传递多个参数

python - 是否有用于查找数字列和分类列的 python 函数?