首页 > 解决方案 > 数字没有加起来

问题描述

我正在尝试制作一个为客户添加信息的作品。这是我的代码:

lovely_loveseat_description = """Lovely Loveseat. Tufted polyester blend on wood. 
32 inches high x 40 inches wide x 30 inches deep. Red or white."""

#We're creating the price for the loveseat

lovely_loveseat_price = 254.00
lovely_loveseat_price = str(254.00)


#Making a new variable

stylish_settee_description = """Stylish Settee. Faux leather on birch. 
29.50 inches high x 54.75 inches wide x 28 inches deep. Black."""
stylish_settee_price = 180.50
stylish_settee_price = str(180.50)

#Making the 3rd and final variable

luxurious_lamp_description = """"Luxurious Lamp. Glass and iron. 36 inches tall. Brown with cream shade."""
luxurious_lamp_price = 52.15
luxurious_lamp_price = str(52.15)

#We're adding sales tax as another variable
sales_tax = .088
customer_one_total = 0
customer_one_itemization = " "
customer_one_itemization += lovely_loveseat_price
customer_one_itemization += luxurious_lamp_price
customer_one_tax = customer_one_total * sales_tax
customer_one_tax += customer_one_total

print("Customer One Items:")
print(str(customer_one_itemization))
print("Customer One Total:")
print(customer_one_total)

输出甚至不是远程正确的,我不知道为什么。我是编程新手。这是输出:

Customer One Items:
 254.052.15
Customer One Total:
0

它是怎么想出来的?

标签: pythonpython-3.xpython-2.7

解决方案


是正确的customer_one_total,从将其设置0customer_one_total = 0

它会输出,254.052.15因为您将它们全部转换为字符串,而不是将它们保留为整数。尝试摆脱str()每个家具变量。

我已经修复了你的代码,顺便说一句,祝你编程好运!有时会变得很艰难,但要坚持下去!

lovely_loveseat_description = """Lovely Loveseat. Tufted polyester blend on wood. 
32 inches high x 40 inches wide x 30 inches deep. Red or white."""

#We're creating the price for the loveseat

lovely_loveseat_price = 254.00


#Making a new variable

stylish_settee_description = """Stylish Settee. Faux leather on birch. 
29.50 inches high x 54.75 inches wide x 28 inches deep. Black."""
stylish_settee_price = 180.50

#Making the 3rd and final variable

luxurious_lamp_description = """"Luxurious Lamp. Glass and iron. 36 inches tall. Brown with cream shade."""
luxurious_lamp_price = 52.15

#We're adding sales tax as another variable
sales_tax = .088
customer_one_total = 0
customer_one_itemization += lovely_loveseat_price
customer_one_itemization += luxurious_lamp_price
customer_one_tax = customer_one_total * sales_tax
customer_one_total += customer_one_tax

print("Customer One Items:")
print(customer_one_itemization)
print("Customer One Total:")
print(customer_one_total)

如果这有帮助,您能否确保接受它,以便其他有相同问题的人可以找到它!


推荐阅读