【问题标题】:How to Refactor multiple elif statements with CSV, i dont know how to approach it im a beginner如何使用 CSV 重构多个 elif 语句,我不知道如何处理它,我是初学者
【发布时间】: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


【解决方案1】:

使用区域索引创建一个包含 csv 文件名称的数组。

然后简单地使用选择的区域作为索引来获取csv文件名:

regions = [ 'Region 1', 'CAR', 'Region II' ]

# do your selection code (you can also loop the regions array for the list)
print( "Please choose your region")
index = 1
for region in regions:
    print(index + " for " + region)
    index++

option = int(input("Your option: "))

regionString = reginos[option-1]
print(regionString)
csv_file = csv.reader(open('file/' + regionString + '.csv', 'r'))
for row in csv_file:
    print(row)
anykey = input("Press any key to return from main menu")
mainMenu()

【讨论】:

    猜你喜欢
    • 2013-08-22
    • 2013-07-28
    • 2020-10-03
    • 1970-01-01
    • 2023-01-26
    • 2021-03-04
    • 2016-06-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多