说明网
数学

请编写一个Python程序,实现全场满200减50的优惠活动

3 篇解答
编写一个程序,让用户输入消费金额,然后根据消费金额判断是否满足满减条件,并输出相应的优惠信息。

ANSWER & EXPLANATION

解答与说明

以下是一个实现全场满200减50优惠活动的Python程序:

python

获取用户输入的商品价格(空格分隔)

prices_input = input("请输入商品价格(用空格分隔):")

将输入转换为浮点数列表

prices = [float(price) for price in prices_input.split()]

计算总金额

total_amount = sum(prices)

应用满减优惠

if total_amount >= 200: final_amount = total_amount - 50 else: final_amount = total_amount

输出结果(保留两位小数)

print(f"商品总金额:{total_amount:.2f}元") print(f"优惠后金额:{final_amount:.2f}元")

运行示例:

请输入商品价格(用空格分隔):120 95 78 商品总金额:293.00元 优惠后金额:243.00元

请输入商品价格(用空格分隔):88 99 商品总金额:187.00元 优惠后金额:187.00元

程序说明:

  1. 接受用户输入的商品价格(支持多个商品价格输入)
  2. 计算商品总金额
  3. 判断是否符合满减条件:
    • 总金额 ≥ 200元:减免50元
    • 总金额 < 200元:保持原价
  4. 输出格式化的金额结果(保留两位小数)

注意事项:

  • 支持输入任意数量的商品价格
  • 自动处理小数点金额
  • 金额显示保留两位小数
  • 满减条件为累计金额(单次购物车总金额)
def calculate_discounted_price(total_amount):
    """
    Calculate the discounted price based on the total amount spent.
    If the total amount is greater than or equal to 200, a discount of 50 is applied.
    
    Parameters:
        total_amount (float): The total amount spent by the customer.
        
    Returns:
        float: The final amount after applying the discount if applicable.
    """
    if total_amount >= 200:
        discount = 50
        discounted_price = total_amount - discount
    else:
        discounted_price = total_amount
    return discounted_price

# Example usage:
total_spent = 250
final_price = calculate_discounted_price(total_spent)
print(f"The final price after discount is: ${final_price}")
# 获取用户输入的消费金额
a = int(input(

内容由 AI 辅助整理,仅供参考;涉及具体版本、时效或个人情况时,请结合适用条件核实。