线性else if语句,如何将其转换为重构代码

2024-06-16 12:53:37 发布

您现在位置:Python中文网/ 问答频道 /正文

顺便说一句,“任意键”未被使用

def load1(): 印刷品( “请选择区域1 \n 1,对于区域1 \n 2,对于区域II\n 4,对于区域II\n 5,对于区域IV\n 6,对于区域VI\n 7,对于区域VI\n 7,对于区域Vi n 8,对于区域Vi \n 9,对于区域VII \n 10,对于CavaGr\Gn的区域VIII \n 11,对于区域IX\n的CiaGa \n,区域X\n,x为区域席“”。 option=int(输入(“您的选项:”) #动作像开关 如果选项==“1”: 打印(“区域1\n”) csv_file=csv.reader(打开('file/Region I.csv','r')) 对于csv_文件中的行: 打印(行) 任意键=输入(“按任意键从主菜单返回”) 主菜单()

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()

Tags: csvin区域forinputopenregionreader
1条回答
网友
1楼 · 发布于 2024-06-16 12:53:37

如果希望选择的内容包含在字典中,请将函数名存储在字典中,然后调用它们:

# 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

相关问题 更多 >