pythonfunctionif-statementargumentsrest-parameters

How can I loop through function arguments?


def grade(*score):
  for i in range(0, score):
    if score >= 90:
      return "A"
    elif score >=80:
      return "B"
    elif score >=70:
      return "C"
    elif score >=60:
      return "D"
    else:
      return "F"

print(grade(87, 92, 100, 54, 72, 84, 81, 74))

I want to be able to loop through the arguments and return the correct grade on each loop.


Solution

  • You can use a for loop, append each grade to a list, and return the list.

    def grade(*score):
    
      grades = []
    
      for i in score:
        if i >= 90:
          grades.append("A")
        elif i >=80:
          grades.append("B")
        elif i >=70:
          grades.append("C")
        elif i >=60:
          grades.append("D")
        else:
          grades.append("F")
    
      return grades
    
    print(grade(87, 92, 100, 54, 72, 84, 81, 74))