python - 为什么我会收到 TypeError : can't multiply sequence by non-int of type 'float' ?

标签 python

我正在输入一个销售金额(通过输入)乘以定义的销售税 (0.08),然后让它打印总金额(销售税乘以销售金额)。

我遇到了这个错误。任何人都知道可能出了什么问题或有任何建议吗?

salesAmount = raw_input (["Insert sale amount here \n"])
['Insert sale amount here \n']20.99
>>> salesTax = 0.08
>>> totalAmount = salesAmount * salesTax

Traceback (most recent call last):
  File "<pyshell#57>", line 1, in <module>
    totalAmount = salesAmount * salesTax
TypeError: can't multiply sequence by non-int of type 'float'

最佳答案

raw_input 返回一个字符串(字符序列)。在 Python 中,字符串和 float 相乘没有明确的含义(而字符串和整数相乘有含义:"AB"* 3 is "ABABAB"; 如何"L"* 3.14 是多少?请不要回复 "LLL|")。您需要将字符串解析为数值。

你可能想试试:

salesAmount = float(raw_input("Insert sale amount here\n"))

关于python - 为什么我会收到 TypeError : can't multiply sequence by non-int of type 'float' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/485789/

相关文章:

python - MaxRetryError Selenium

python - python中的列表和文件

python - 来自 Flask 端点的长时间运行的脚本

python - 定义元组/列表时的条件

python - Sympy 点积无法正确读取尺寸

python - 从标准泰国舞蹈 wav 歌曲中检测钹(泰国乐器)节奏

python - PyBrain 弃用警告

python - 使用 Python 脚本在 MS SQL 数据库中创建表时出现问题

python - 为什么这个操作在 CPU 上比在 GPU 上执行得更快?

python - 我们可以从 AWS Lambda 访问所有账户堆栈及其信息吗?