python - 如何在python中修复“ValueError:无法将字符串转换为浮点数:'0180.5052.15'”
问题描述
我正在练习用 python 编写代码,我学习的网站让我做了这个项目,我为在虚构商店购买东西的客户创建收据。
我目前正在 CodeAcademy 上学习 python,当我运行代码时,它给了我第 33 行的错误。第 33 行中的原始代码没有将字符串转换为浮点数,所以我将它转换为浮点数,但它给了我一个不同的错误。
# description of item 1
lovely_loveseat_description = "Lovely Loveseat. Tufted polyester blend on wood. 32 inches high x 40 inches wide x 30 inches deep. Red or white."
# price of item 1
lovely_loveseat_price = "180.50"
# description of item 2
stylish_settee_description = "Stylish Settee. Faux leather on birch. 29.50 inches high x 54.75 inches wide x 28 inches deep. Black."
# price of item 2
stylish_settee_price = "180.50"
# description of item 3
luxurious_lamp_description = "Luxurious Lamp. Glass and iron. 36 inches tall. Brown with cream shade."
# price of item 3
luxurious_lamp_price = "52.15"
# sales tax is 8.8%
sales_tax = "0.088"
# second customer
customer_two_total = "0"
# customer 2 list of items
customer_two_itemization = "stylish_settee_description + luxurious_lamp_description"
# customer 2 total before tax
customer_two_total += stylish_settee_price
customer_two_total += luxurious_lamp_price
# customer 2 tax
customer_two_tax = float(customer_two_total) * float(sales_tax)
# customer 2 total after tax
customer_two_total += customer_two_tax
# customer 2 receipt
print("Customer Two Items:")
print(customer_two_itemization)
print("Customer Two Total:")
print(customer_two_total)
我无法弄清楚我做错了什么,因为它给了我这个确切的错误,
Traceback (most recent call last):
File "script.py", line 33, in <module>
customer_two_tax = float(customer_two_total) * float(sales_tax)
ValueError: could not convert string to float: '0180.5052.15'
解决方案
推荐阅读
- amazon-web-services - AWS - 无法从 EC2 访问 S3 存储桶对象
- r - knitr Minimal Demo 编译不正确
- quarkus - 在 SecurityIdentityAugmentor 中注入 HTTP 请求
- tensorflow - 使用 tensorflow HMC 边缘化 GPR 超参数时出错
- charts - 如何在同一个 Mendix 页面上显示两个饼图?
- angular - 数据路径“.builders ['app-shell']”应该具有必需的属性'class'当我试图使用带有角度的firebase时我得到了这个错误
- python - 基于 .txt 文件的 os.walk 文件夹排除
- python - 当我使用 Python 有一个未知数时,如何求解两个彼此相等的方程?
- mongodb - 使用 mongodb 将记录合并为一个
- reactjs - 如何在基本应用程序组件中使用 GraphQL 挂钩进行响应而不重新渲染?