例外:"不能将序列乘以'int'类型的非int

bas*_*ath 2 python exception-handling exception python-3.x

我正在尝试编写非常简单的代码来计算文章的折扣.以下是代码,它会在代码的第3行引发异常.例外是:

Traceback (most recent call last):
  File "C:/Users/basam/AppData/Local/Programs/Python/Python35/discounts.py", line 3, in <module>
    discount=0.1*price
TypeError: can't multiply sequence by non-int of type 'float'
Run Code Online (Sandbox Code Playgroud)

代码:

price=input('how much is your item?')
if int(price) <= 10:
    discount=0.1*price
Run Code Online (Sandbox Code Playgroud)

有人可以提出问题的建议吗?

mtr*_*trw 5

关键是int(price).该input命令返回存储在变量中的字符串price.

在第二行中,price通过调用将其转换为数字int(price),但该结果不会存储在任何位置.它用于比较然后丢弃.因此,当你在第三行中相乘时,你试图将一个数字乘以一个字符串.