这段代码是简体吗?我应该使用更多功能吗?
问题描述:
所以我正在开发这个程序,它打开一个外部文件,然后运行它以查看它是否包含特定信息。有没有一种方法来简化它,或者它是现在写这个最有效的方式吗?这段代码是简体吗?我应该使用更多功能吗?
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...
我很好奇,看看是否有更短的方法来获得相同的结果。也许使用运行那段代码的函数,但我不知道该怎么做。
答
一些问题需要注意的:
-
构建
是非常讨厌的;如果你确实需要
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()
这应该是在代码审查。 –
我投票结束这个问题作为题外话,因为它属于[codereview.se] –