Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用列表计算等级Python 3,有助于简化代码_Python_Python 3.x_List_Loops_For Loop - Fatal编程技术网

使用列表计算等级Python 3,有助于简化代码

使用列表计算等级Python 3,有助于简化代码,python,python-3.x,list,loops,for-loop,Python,Python 3.x,List,Loops,For Loop,我的家庭作业需要帮助/输入。我已经有了一个可以工作的代码,但我想知道是否有一种更简单的方法来编写代码,使其不那么冗长 以下是作业说明,以供参考: 有十个名字和五份考试成绩表。通信 姓名和考试成绩之间的差距由职位决定。例如,Cindy的考试分数是67、92、67、43、78。从每个学生的五个考试分数中取最低的一个,取其余分数的平均值,然后确定该学生的字母分数。确保您的打印输出与我的相同,列宽相同 这是我的密码: names = ['April','Bill','Cindy','Dave','Emi

我的家庭作业需要帮助/输入。我已经有了一个可以工作的代码,但我想知道是否有一种更简单的方法来编写代码,使其不那么冗长

以下是作业说明,以供参考:

有十个名字和五份考试成绩表。通信 姓名和考试成绩之间的差距由职位决定。例如,Cindy的考试分数是67、92、67、43、78。从每个学生的五个考试分数中取最低的一个,取其余分数的平均值,然后确定该学生的字母分数。确保您的打印输出与我的相同,列宽相同

这是我的密码:

names = ['April','Bill','Cindy','Dave','Emily',  'Frank','Gene','Hank','Irene','Jeff']
test1 = [34,21,67,45,88,  77,63,96,89,88]
test2 = [11,67,92,35,89,  25,78,94,81,63]
test3 = [94,33,67,34,67,  88,55,99,23,43]
test4 = [27,83,43,67,93,  45,67,77,86,90]
test5 = [43,76,78,45,65,  99,65,65,79,43]

total = [0,0,0,0,0,0,0,0,0,0]
min = [0,0,0,0,0,0,0,0,0,0]
percent = [0,0,0,0,0,0,0,0,0,0]
grade = ['F','F','F','F','F','F','F','F','F','F']

for i in range (10):
    total[i] = test1[i]
    min[i] = test1[i]
    
for i in range (10):
    total[i] = total[i] + test2[i]
    total[i] = total[i] + test3[i]
    total[i] = total[i] + test4[i]
    total[i] = total[i] + test5[i]
    min[i] = min[i] if min[i] < test2[i] else test2[i]
    min[i] = min[i] if min[i] < test3[i] else test3[i]
    min[i] = min[i] if min[i] < test4[i] else test4[i]
    min[i] = min[i] if min[i] < test5[i] else test5[i]
    total[i] = total[i] - min[i]
    
for i in range (10):
    percent[i] = total[i]/4.0
    if percent[i] >= 90: grade[i] = 'A'
    elif percent[i] >= 80: grade[i] = 'B'
    elif percent[i] >= 70: grade[i] = 'C'
    elif percent[i] >= 60: grade[i] = 'D'
    elif percent[i] >= 50: grade[i] = 'F'
    print (" %s\t%d %2.2f %s \n" %(names[i], total[i], percent[i], grade[i]))

基本上,我只是想知道是否有人有任何想法/方法供我尝试,以简化代码。我只是感到困惑,因为分数是垂直的。这两种方式都很好,但我觉得它可以修改。我还是一个初学者,所以我可能不熟悉什么是简单的修复:)谢谢

这是字典而不是列表的理想用例。字典允许您为键指定值。在这种情况下,关键是学生姓名及其分数。例如:

{student_1 : [grade_1, grade_2 ... grade_n],
 ...
 student_n : [grade_1, grade_2 ... grade_n]
}
通过这种方式,将分数与任何给定的学生联系起来要容易得多

此外,一些非常的基本功能定义(例如平均等级)在这里也非常有用。事实证明,使用f-string创建干净的输出非常有用

总之:查找字典、函数和f字符串可以教给您许多有用的技能,以便进一步编码。我在下面举了一个例子,希望能理解

names = ['April','Bill','Cindy','Dave','Emily', 

'Frank','Gene','Hank','Irene','Jeff']

test1 = [34,21,67,45,88,  77,63,96,89,88]
test2 = [11,67,92,35,89,  25,78,94,81,63]
test3 = [94,33,67,34,67,  88,55,99,23,43]
test4 = [27,83,43,67,93,  45,67,77,86,90]
test5 = [43,76,78,45,65,  99,65,65,79,43]

#Assign every student to the dictionary
#So far their respective grades are an empty list
grades = dict()
for name in names:
    grades[name] = []

#Group all tests in a single list, which is easier to iterate over
tests = [test1, test2, test3, test4, test5]

#Loop over all tests
#Every i-th student gets the i-th value of every test added to the dictionary
for test in tests:
    for i in range(len(test)):
        grades[names[i]].append(test[i])

#Remove the lowest grade and calculate the average afterwards
def avg_grade(grades):
    grades.remove(min(grades))
    return sum(grades) / len(grades)

#Associate a grade with a letter compared to a threshold
def letter(grade):
    thresholds = [90, 80, 70, 60, 50]
    letters = ['A', 'B', 'C', 'D', 'F']
    for i in range(len(thresholds)):
        if grade >= thresholds[i]:
            return letters[i]

#Output as f-string
for name in names:
    print(f'{name:6} {avg_grade(grades[name]):.2f}  '
          f'{letter(avg_grade(grades[name]))}')

来看看你的问题是否可以作为主题。@khelwood谢谢。我应该把它转寄到那里吗?我没有经常使用堆栈溢出:)如果你认为你可以为codereview写一个好问题,那就继续吧,但一定要先阅读他们的页面。这个问题对于堆栈溢出来说可能过于模糊;
names = ['April','Bill','Cindy','Dave','Emily', 

'Frank','Gene','Hank','Irene','Jeff']

test1 = [34,21,67,45,88,  77,63,96,89,88]
test2 = [11,67,92,35,89,  25,78,94,81,63]
test3 = [94,33,67,34,67,  88,55,99,23,43]
test4 = [27,83,43,67,93,  45,67,77,86,90]
test5 = [43,76,78,45,65,  99,65,65,79,43]

#Assign every student to the dictionary
#So far their respective grades are an empty list
grades = dict()
for name in names:
    grades[name] = []

#Group all tests in a single list, which is easier to iterate over
tests = [test1, test2, test3, test4, test5]

#Loop over all tests
#Every i-th student gets the i-th value of every test added to the dictionary
for test in tests:
    for i in range(len(test)):
        grades[names[i]].append(test[i])

#Remove the lowest grade and calculate the average afterwards
def avg_grade(grades):
    grades.remove(min(grades))
    return sum(grades) / len(grades)

#Associate a grade with a letter compared to a threshold
def letter(grade):
    thresholds = [90, 80, 70, 60, 50]
    letters = ['A', 'B', 'C', 'D', 'F']
    for i in range(len(thresholds)):
        if grade >= thresholds[i]:
            return letters[i]

#Output as f-string
for name in names:
    print(f'{name:6} {avg_grade(grades[name]):.2f}  '
          f'{letter(avg_grade(grades[name]))}')