首页 > 解决方案 > 如何在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'

标签: pythonpython-3.x

解决方案


推荐阅读