Python使用getattr调用函数不再用If..else
import sys
def book_insert():
pass
def student_insert():
pass
def ...():
...
if __name__ == '__main__':
funcs_list = ['book_insert', 'student_insert', 'load_infos', 'borrow_books', 'return_books', 'borrow_query',
'student_query', 'amount_books', 'visual_data']
user_options = ['图书信息录入', '学生信息录入', '数据读取', '借阅', '归还', '查询借阅信息', '查询学生信息', '数量统计','数据可视化']
while True:
print('图书管理系统'.center(30, '-'))
for index, opt in enumerate(user_options, 1):
print(index, opt)
choose_num = input('请选择:')
choose_num = int(choose_num)
try:
getattr(sys.modules[__name__], funcs_list[choose_num - 1])()
except Exception as err:
print(err)
print('选择功能发生错误')
break