数字不累加

时间:2018-11-26 20:37:45

标签: python python-3.x python-2.7

我正在尝试制作一块为客户增加信息的地方。这是我的代码:

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

怎么想呢?

1 个答案:

答案 0 :(得分:0)

customer_one_total是正确的,与使用0将其设置为customer_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)

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