python - 线性 else if 语句,我如何将其转换为重构代码
问题描述
顺便说一句,“anykey”尚未使用
def load1(): print( "请选择您的地区\n 1 代表地区 1\n 2 代表 CAR \n 3 代表地区 II\n 4 代表地区 III\n 5 代表地区 IV\n 6 代表 NCR\n 7 代表区域 V\n 区域 VI 为 8\n 区域 VII 为 9\n 区域 VII 为 10\n 区域 VIII 为 11\n 区域 VIII 为 12\n 区域 IX 为 13\n 区域 X 为 14\n 区域 XI 为 15") option = int(input("Your option: ")) # 作用类似于 switch if option == "1": print("Region 1\n") csv_file = csv.reader(open('file/Region I.csv' , 'r')) for row in csv_file: print(row) anykey = input("Press any key to return from main menu") mainMenu()
elif option == "2":
print("CAR\n")
csv_file = csv.reader(open('file/CAR.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == "3":
print("Region 2\n")
csv_file = csv.reader(open('file/Region II.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 4:
print("Region 3\n")
csv_file = csv.reader(open('file/Region III.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 5:
print("Region 4\n")
csv_file = csv.reader(open('file/Region IV.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 6:
print("NCR 4\n")
csv_file = csv.reader(open('file/NCR.csv', 'r'))
for row in csv_file:
print(row)
print()
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 7:
print("Region 5\n")
csv_file = csv.reader(open('file/Region V.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 8:
print("Region 6\n")
csv_file = csv.reader(open('file/Region VI.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 9:
print("Region 7\n")
csv_file = csv.reader(open('file/Region VII.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 10:
print("SOCCSKARGEN\n")
csv_file = csv.reader(open('file/SOCCSKARGEN.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 11:
print("Region 8\n")
csv_file = csv.reader(open('file/Region VIII.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 12:
print("CARAGA\n")
csv_file = csv.reader(open('file/CARAGA.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 13:
print("Region 9\n")
csv_file = csv.reader(open('file/Region IX.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 14:
print("Region 10\n")
csv_file = csv.reader(open('file/Region X.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
elif option == 15:
print("Region 11\n")
csv_file = csv.reader(open('file/Region XI.csv', 'r'))
for row in csv_file:
print(row)
anykey = input("Press any key to return from main menu")
mainMenu()
解决方案
如果您希望您的选择在字典中,请将函数名称存储在其中,然后调用它们:
# load(), search() and mainMenu() are defined above somewhere
def print_and_exit():
print("Thank you for using the program.")
exit()
def invalid_choice():
print("Invalid choice, please select from 1 to 3")
mainMenu()
selections={"1":load1, "2":search, "3":print_and_exit}
def select(selection):
# call the function associated with the selection, or indicate that an invalid choice was made
selections.get(selection, invalid_choice)() # invalid_choice() is called if the choice is not in the selections dictionary
推荐阅读
- php - PHP从mysqli查询中保存数组,索引为数组键
- r - 正则表达式提取前 n 位数字和所有字母
- r - 数据表中的高效条件交叉连接
- java - 改变hibernate查找相关数据的方式?
- angular - Angular 6 构建错误预期 0 个参数,但在形式上得到 1
- scala - 在 Spark scala 中创建 ArrayType 列
- javascript - 从对象中获取属性值并将其分配给javascript中的变量
- sql - 访问查询中的分组加权平均
- python-3.x - 在 AWS Sagemaker 上安装 graphiz
- angular - Angular:使用 ChangeDetectionStrategy.OnPush 进行模型侦听