薪资系统代码补充需求:添加返回主菜单与退出确认逻辑
薪资系统核心功能补充代码方案
针对你提到的薪资系统缺失的两个核心功能,这里给出具体的代码补充实现:
1. 操作后询问是否继续,提供返回主菜单/完全退出选项
先封装一个通用的询问函数,方便在所有输入/计算操作完成后调用:
def continue_or_exit(): while True: choice = input("\n是否继续当前操作?(Y/N): ").strip().upper() if choice == 'Y': return True # 返回True,继续当前操作流程 elif choice == 'N': # 弹出二级选择菜单 while True: opt = input("请选择后续操作:[1]返回主菜单 [2]完全退出程序: ").strip() if opt == '1': return False # 返回False,触发回到主菜单逻辑 elif opt == '2': print("程序已安全退出") exit() # 直接终止程序 else: print("无效选项,请重新输入") else: print("输入错误,请输入Y或N")
然后在每个输入/计算函数的末尾调用这个函数,比如输入员工信息的函数:
def input_employee_info(): # 原有输入员工信息的逻辑代码... print("员工信息录入完成") # 调用询问函数,如果返回False则直接结束当前函数,回到主菜单 if not continue_or_exit(): return
2. 主菜单选择Exit时的确认逻辑
在主菜单的选项处理分支中,针对[5]Exit选项添加确认步骤:
def main_menu(): while True: print("\n=== 薪资系统主菜单 ===") print("[1] 录入员工信息") print("[2] 计算员工薪资") print("[3] 查看薪资报表") print("[4] 修改员工信息") print("[5] 退出系统") user_choice = input("请输入选项编号: ").strip() if user_choice == '1': input_employee_info() elif user_choice == '2': calculate_salary() elif user_choice == '3': view_salary_report() elif user_choice == '4': modify_employee_info() elif user_choice == '5': # 添加退出确认逻辑 while True: confirm = input("确认要退出薪资系统吗?(Y/N): ").strip().upper() if confirm == 'Y': print("感谢使用薪资系统,再见!") exit() elif confirm == 'N': break # 回到主菜单循环 else: print("输入无效,请输入Y或N") else: print("无效选项,请重新输入")
内容的提问来源于stack exchange,提问作者EnBaby
相关产品推荐
相关产品推荐

