【发布时间】:2021-01-16 17:20:37
【问题描述】:
(过程中没有用到“anykey”)
定义加载1(): 打印( "请选择您的地区\n 1 代表地区 1\n 2 代表 CAR \n 3 代表地区 II\n 4 代表地区 III\n 5 代表地区 IV\n 6 代表 NCR\n 7 代表地区 V\n 8 代表地区VI\n 9 区 VII\n 10 区 SOCCSKARGEN\n 11 区 VIII\n 12 区 CARAGA\n 13 区 IX\n 14 区 X\n 15 区 XI ") option = int(input("你的选择:")) # 像开关一样 如果选项 == 1: print("区域 1\n") csv_file = csv.reader(open('file/Region I.csv', 'r')) 对于 csv_file 中的行: 打印(行) anykey = input("按任意键从主菜单返回") 主菜单()
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()
【问题讨论】:
-
这个方法是在调用时加载的
-
好吧。你说你不知道如何处理它(重构),所以我会给你一些提示:寻找重复的代码,比如
csv.reader(open('file/Region [x].csv', 'r')和anykey = input("Press any key to return from main menu")。为什么不创建一个简单的函数,将文件名作为参数并打印内容 + 静态字符串 + 调用 mainMenu(),在每个 elif 中重复?您可以使用 dict 来保存整数输入 -> 文件名的映射,并使用 switch 语句来调用此函数而不是所有 elifs?只是一些想法......
标签: python refactoring