A 商店准备在今年夏天开始出售西瓜西瓜的售价如下20 斤以上的每斤 085 元;重于 15 斤轻于等于 20 斤的每斤 090 元;重于 10 斤轻于等于 15 斤的每斤 095 元;重于 5 斤轻于等于 10 斤的每斤 100 元;轻于或等于 5 斤的每斤 105 元。现在为了知道商店是否会盈利要求 A 公司帮忙设计一个输入西瓜的重量和顾客所付钱数输出应付货款和应找钱数的程序
输入: 西瓜重量(单位:斤):25 顾客付款(单位:元):20
输出: 应付货款:21.25 元 应找钱数:-1.25 元
输入: 西瓜重量(单位:斤):7 顾客付款(单位:元):8.5
输出: 应付货款:7.00 元 应找钱数:1.50 元
代码如下:
weight = float(input("西瓜重量(单位:斤):")) payment = float(input("顾客付款(单位:元):"))
if weight > 20: price = 0.85 elif weight > 15: price = 0.90 elif weight > 10: price = 0.95 elif weight > 5: price = 1.00 else: price = 1.05
total_cost = round(price * weight, 2) change = round(payment - total_cost, 2)
print("应付货款:{} 元".format(total_cost)) print("应找钱数:{} 元".format(change))
原文地址: http://www.cveoy.top/t/topic/KRQ 著作权归作者所有。请勿转载和采集!