Python月度账单程序优化:为月份与小时输入添加循环
问题
我是Python新手,写了个计算客户月度账单的程序,需要获取客户购买的套餐(A/B/C)、数字形式的月份及使用时长,计算并显示应付总金额,且使用时长不能超过每月720小时(按30天算)。套餐规则如下:
- Package A:每月Php200.00,包含10小时时长,超出部分每小时Php15.00;
- Package B:每月Php500.00,包含20小时时长,超出部分每小时Php10.00;
- Package C:每月Php900.00,不限时长。
现有代码功能正常,但想给月份和小时的输入加循环,输入错误时重复提示,直到输入正确再继续。不知道该在哪加while循环,现有代码如下:
price_A = 200.00 price_B = 500.00 price_C = 900.00 Package = "" package = False while Package not in ("A", "a", "B", "b", "C", "c"): Package = input("Enter package: ") package = True if Package in ("A", "a"): month = int(input("Enter month: ")) if int(float(month)) >= 1 and int(float(month)) <= 12: hours = int(input("Enter hours: ")) if int(float(hours)) >= 0 and int(float(hours)) <= 720: if int(float(hours)) >= 0 and int(float(hours)) <= 10: total = price_A f_total = "{:.2f}".format(total) print("Total amount due is Php " + f_total) elif int(float(hours)) >= 10: total = ((hours - 10) * 15.00) + price_A f_total = "{:.2f}".format(total) print("Total amount due is Php " + f_total) else: print("Invalid Hours!") else: print("Invalid Month!") elif Package in ("B", "b"): month = int(input("Enter month: ")) if int(float(month)) >= 1 and int(float(month)) <= 12: hours = int(input("Enter hours: ")) if int(float(hours)) >= 0 and int(float(hours)) <= 720: if int(float(hours)) >= 0 and int(float(hours)) <= 20: total = price_B f_total = "{:.2f}".format(total) print("Total amount due is Php " + f_total) elif int(float(hours)) >= 20: total = ((hours - 10) * 10.00) + price_B f_total = "{:.2f}".format(total) print("Total amount due is Php " + f_total) else: print("Invalid Hours!") else: print("Invalid Month!") elif Package in ("C", "c"): f_total = "{:.2f}".format(price_C) print("Total amount due is Php " + str(f_total)) else: print("Invalid Package!")
解决方案
要实现输入错误时重复提示的功能,需将月份和小时的输入逻辑用while循环包裹,确保只有输入合法后才进入计算环节。同时优化原代码的重复结构,修正计算错误,最终代码如下:
price_A = 200.00 price_B = 500.00 price_C = 900.00 # 获取合法套餐 package = "" while package not in ("A", "a", "B", "b", "C", "c"): package = input("Enter package: ") if package not in ("A", "a", "B", "b", "C", "c"): print("Invalid Package!") # 处理套餐A和B的逻辑 if package.lower() in ("a", "b"): # 获取合法月份:循环直到输入1-12的整数 month = None while month is None: try: month_input = input("Enter month: ") month = int(month_input) if not (1 <= month <= 12): print("Invalid Month! Please enter a number between 1 and 12.") month = None except ValueError: print("Invalid Month! Please enter a valid integer.") # 获取合法时长:循环直到输入0-720的整数 hours = None while hours is None: try: hours_input = input("Enter hours: ") hours = int(hours_input) if not (0 <= hours <= 720): print("Invalid Hours! Please enter a number between 0 and 720.") hours = None except ValueError: print("Invalid Hours! Please enter a valid integer.") # 计算账单 if package.lower() == "a": total = price_A if hours <= 10 else price_A + (hours - 10) * 15.00 else: # 套餐B total = price_B if hours <= 20 else price_B + (hours - 20) * 10.00 f_total = "{:.2f}".format(total) print(f"Total amount due is Php {f_total}") # 处理套餐C的逻辑 else: f_total = "{:.2f}".format(price_C) print(f"Total amount due is Php {f_total}")
关键改动说明
- 独立输入循环:为月份、小时分别添加
while循环,配合try-except捕获非整数输入,非法输入时持续提示直到输入符合要求。 - 修正计算错误:原代码中Package B超出时长计算时错误减去10,改为减去套餐包含的20小时,符合规则。
- 代码去重:将A/B套餐的输入逻辑合并,避免重复编写相同的输入校验代码。
- 简化输出:使用f-string替代字符串拼接,让格式化输出代码更简洁易读。
内容的提问来源于stack exchange,提问作者canariiiii
相关产品推荐
相关产品推荐

