python - 数字没有加起来
问题描述
我正在尝试制作一个为客户添加信息的作品。这是我的代码:
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
它是怎么想出来的?
解决方案
是正确的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)
如果这有帮助,您能否确保接受它,以便其他有相同问题的人可以找到它!
推荐阅读
- excel - 如何计算两个单元格之间的时间差(以秒为单位),其中单元格格式为“yyyy-mm-dd hh:mm:ss”格式的文本?
- html - 我的页脚未对齐,需要修复以使其位于中心
- excel - 将pdf导出到excel
- atom-editor - Live Server 已停止
- python - Series 的真值是模棱两可的。对 np.argmax 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
- sql - 解析来自肥皂网络服务的 xml 响应以保存在表中
- javascript - Google Maps V3 DrawingManager 不记得可编辑/可拖动标志
- c# - 定义一个 WebAPI 方法,该方法的限制少于其授权类
- django - 指定的未知字段 ()
- ocaml - 创建聊天服务器