简体   繁体   English

函数内的 for 循环

[英]For-loop within a function

I am currently taking a short course on python and am having a bit of trouble with an assignment.我目前正在参加有关 Python 的短期课程,但在作业时遇到了一些麻烦。

The task is to create a function that takes a list of names and marks eg [Student 1, 35, Student 2, 78] etc as an argument and outputs a grade based on a marking scheme.任务是创建一个函数,该函数将姓名和分数列表(例如 [Student 1, 35, Student 2, 78] 等)作为参数,并根据评分方案输出成绩。

I have working code but i feel like its not quite satisfying the question.我有工作代码,但我觉得它不太满意这个问题。 I have defined a function that determines the grade and then use a for-loop to call that function and apply it to every item in a list.我定义了一个确定等级的函数,然后使用 for 循环调用该函数并将其应用于列表中的每个项目。 I feel like there is a more elegant solution where the function is doing the loop but am struggling to get it to work.我觉得有一个更优雅的解决方案,该函数正在执行循环,但正在努力使其工作。 I believe its something to do with the variables in the function being local and not global.我相信这与函数中的变量是局部的而不是全局的有关。

data = ["Fozzie", 34, "Kermit", 78, "Miss Piggy", 23, "Gonzo", 55, "Beaker", 88, "Honeydew", 59, "Animal", 10, "Rowlf", 54]

def assign_grades(source):
    student_name = str(source[student])
    mark = int(source[student + 1])
    grade = str("")
  
        
    if mark < 40:
        grade = "Fail"
    elif mark < 60:
        grade = "Pass"
    elif mark < 70:
        grade = "Merit"
    elif mark >= 70:
        grade = "Distinction"
    else:
        grade = "Error"


    return("---------------------------------------------"
          +"\nStudent Name: "
          +str(student_name)
          + "\nMark: "
          + str(mark)
          + "\nGrade: " + str(grade)
          +"\n---------------------------------------------")


for student in range(0, len(data), 2):
    print(assign_grades(data))


 

I'm not sure if this is cleaner or just more cryptic, but it uses less code and no global variables.我不确定这是否更清晰或更神秘,但它使用更少的代码并且没有全局变量。

def get_marks():
    # get marks from database or user input
    # for testing, just return list
    mks = [("Fozzie", 34), ("Kermit", 78), ("Miss Piggy", 23), ("Gonzo", 55), ("Beaker", 88), ("Honeydew", 59), ("Animal", 10), ("Rowlf", 54)]
    return mks

def assign_grade(mark): # convert mark to grade
    lmts = [(40,'Fail'),(60,'Pass'),(70,'Merit'),(200,'Distinction')] # mark never over 200
    return [x[1] for x in lmts if x[0] >= mark][0]  # return first matching range

marks = get_marks()  # get name/mark data
grades = [assign_grade(m[1]) for m in marks] # pass/fail/...

for g in zip(marks,grades):  # merge name/mark/grade
    print ("---------------------------------------------"
       +"\nStudent Name: "
       +str(g[0][0])
       + "\nMark: "
       + str(g[0][1])
       + "\nGrade: " + str(g[1])
       +"\n---------------------------------------------")
          

Output输出

---------------------------------------------
Student Name: Fozzie
Mark: 34
Grade: Fail
---------------------------------------------
---------------------------------------------
Student Name: Kermit
Mark: 78
Grade: Distinction
---------------------------------------------
.......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM