2015-11-04 58 views
-1

所以我正在開發這個程序,它打開一個外部文件,然後運行它以查看它是否包含特定信息。有沒有一種方法來簡化它,或者它是現在寫這個最有效的方式嗎?這段代碼是簡體嗎?我應該使用更多功能嗎?

def printGender(alist): 
    if "Female" in alist: 
     print(alist) 
     print("Female Students") 

def maleInfo(blist): 
    if "2010" in blist: 
     print(blist) 
     print("Students who enrolled in 2010") 

def csc2010(clist): 
    if "CSC" in clist and "2010" in clist and "Female" in clist: 
     print(clist) 
     print("Female students who registered in CSC in 2010") 

def main(): 
    ref = open("file1.txt","r") 

    studentList = ref.readlines() 
    ask = 10 
    while ask != 0: 
    print("1) print all female info") 
    print("2) display all male info from 2010") 
    print("3) display female students who registered for CSC in 2010") 
    ask = int(input("Enter option 1, 2, 3 or 0 to quit: ")) 
    if ask == 1: 
     for i in range(len(studentList)): 
      alist = studentList[i] 
      printGender(alist) 
    elif ask == 2: 
     for i in range(len(studentList)): 
      blist = studentList[i] 
      maleInfo(blist) 
    elif ask == 3: 
     for i in range(len(studentList)): 
      clist = studentList[i] 
      csc2010(clist) 
    elif ask == 0: 
     print("You chose to quit") 
     break 
    else: 
     print("Not a Valid input") 
     continue 

    ref.close() 

main() 

有沒有簡化此代碼的方法,以便我不在主函數中創建三個獨立列表。

if ask == 1: 
     for i in range(len(studentList)): 
      alist = studentList[i] 
      printGender(alist) 
    elif ask == 2: 
     for i in range(len(studentList)): 
      blist = studentList[i] 
      maleInfo(blist) 
    elif ask == 3: 
     for i in range(len(studentList)): 
      clist = studentList[i] 
      csc2010(clist) 
    elif ask == 0: 
     print("You chose to quit") 
     break 
    else: 
    ect... 

我很好奇,看看是否有更短的方法來獲得相同的結果。也許使用運行那段代碼的函數,但我不知道該怎麼做。

+3

這應該是在代碼審查。 –

+1

我投票結束這個問題作爲題外話,因爲它屬於[codereview.se] –

回答

1

一些問題需要注意的:

  • 構建

    ​​

    是非常討厭的;如果你確實需要i你應該使用

    for i, student in enumerate(student_list): 
        print_gender(student) 
    

    否則

    for student in student_list: 
        print_gender(student) 
    
  • 你的函數命名不好;他們不會做他們所說的事! printGender列印女學生,printMale列印2010年的學生等。同樣,您的變數名稱選擇不當; alist不是學生名單,它是單身學生。

  • 你似乎有一個每個學生的文本字符串,在猜測像2009, 176915, Jones, Susan, Female, CSC;但你不會試圖分離出來的領域。這將導致像2009, 292010, Male, Jill, Female, RCSCA這樣的學生在2009年和2010年都會報告爲學生的問題(學生編號錯誤匹配),以及女性和男性(姓氏錯誤匹配)以及CSC(錯誤匹配課程名)。您真的需要使用更好的數據格式 - 無論是.csv還是.json還是數據庫,任何返回命名字段的數據 - 都可以解決此問題。

  • 您的搜索選項是非正交的並且僅限於預先編碼的選項;例如,您無法在2007年爲所有CSC學生進行搜索,而無需重新編寫程序。

修復這些問題會導致你喜歡的東西

import json 

def record_print_format(record): 
    return "{Year:4} {Id:6} {Gender:6} {Firstname:>20} {Lastname:<20} {Course:6}".format(**record) 

def show_records(records, format_fn=record_print_format): 
    for r in records: 
     print(format_fn(r)) 
    num = len(records) 
    print("{} records:".format(num)) 

def filter_records(records, field, value): 
    return [r for r in records if r[field] == value] 

def match_year(records, year): 
    return filter_records(records, "Year", year) 

def match_gender(records, gender): 
    return filter_records(records, "Gender", gender) 

def match_course(records, course): 
    return filter_records(records, "Course", course) 

def main(): 
    with open("student_list.json") as studentfile: 
     all_students = json.load(studentfile) 
     records = all_students 

    while True: 
     print("1: Filter by year") 
     print("2: Filter by gender") 
     print("3: Filter by course") 
     print("8: Show results") 
     print("9: Clear filters") 
     print("0: Exit") 

     option = input("Please pick an option: ").strip() 

     if option == "1": 
      year = input("Which year? ").strip() 
      records = match_year(records, year) 
     elif option == "2": 
      gender = input("Which gender? [Male|Female] ").strip() 
      records = match_gender(records, gender) 
     elif option == "3": 
      course = input("Which course? ").strip() 
      records = match_course(records, course) 
     elif option == "8": 
      show_records(records) 
     elif option == "9": 
      records = all_students 
     elif option == "0": 
      print("Goodbye!") 
      break 
     else: 
      print("I don't recognize that option.") 

if __name__=="__main__": 
    main()