提交时间:2024-12-24 14:48:10

运行 ID: 50461

def calculate_discount(total_amount): """ 根据总金额计算折扣后的价格 :param total_amount: 总金额 :return: 折扣后的价格 """ if total_amount >= 4000: discount_rate = 0.6 elif total_amount >= 3000: discount_rate = 0.7 elif total_amount >= 2000: discount_rate = 0.8 else: discount_rate = 1.0 discounted_price = total_amount * discount_rate return discounted_price def main(): try: total_amount = float(input("请输入购物总金额: ")) if total_amount < 0: print("输入的金额不能为负数,请重新输入。") return final_price = calculate_discount(total_amount) print(f"原价: {total_amount}元") print(f"折扣后的价格: {final_price:.2f}元") except ValueError: print("输入无效,请输入一个数字。") if __name__ == "__main__": main()