某产品的价格是800元/件,但若购买量大可给予一定的折扣:够100(含100,下同)件时打9折,够200件打85折,够300件打82折,够500件打8折,请用嵌套的if-else语句编程求解应收款。 不允许使用并列(平等)的if,即:所有的if必须都嵌套
时间: 2024-10-28 21:12:05 浏览: 9
要编写一个满足条件的嵌套if-else语句来计算购买特定数量商品后的应收款,可以按照以下步骤进行:
```python
# 定义商品价格和折扣率
price_per_item = 800
discount_rates = {
100: 0.9,
200: 0.85,
300: 0.82,
500: 0.8
}
def calculate_total_amount(quantity):
if quantity >= 500:
discount_rate = discount_rates[500]
elif quantity >= 300:
discount_rate = discount_rates[300]
elif quantity >= 200:
discount_rate = discount_rates[200]
elif quantity >= 100:
discount_rate = discount_rates[100]
else:
# 如果购买数量小于100件,则按原价计算,无折扣
discount_rate = 1
total = quantity * price_per_item * discount_rate
return total
# 示例:计算购买400件商品的总金额
quantity_to_buy = 400
total_amount = calculate_total_amount(quantity_to_buy)
print(f"购买 {quantity_to_buy} 件商品的应收款为:{total_amount}元")
#
阅读全文