Python 3- Assign grade

后端 未结 2 1599
别跟我提以往
别跟我提以往 2021-01-29 14:00

I am trying to write a program that reads a list of scores and then assigns a letter grade based on the score. Define a function to prompt user to enter valid scores until they

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 14:57

    That's what you're looking for:

    from statistics import mean
    
    def main():
       grades = getScores()
       grade = mean(grades)
       abcGrade = getGrade(grade)
       print(grade, "is an", abcGrade)
    
    
    def getScores():
       grades = []
       while True:
          grade = int(input("Enter grades (-999 ends): "))
          if grade == -999: return grades
          grades.append(grade)
    
    def getGrade(grade):
       best = 100
       if grade >= best - 10:
          return 'A'
       elif grade >=  best - 20:
          return 'B'
       elif grade >= best - 30:
          return 'C'
       elif grade >= best - 40:
          return 'D'
       else:
          return 'F'
    
    main()
    

    I've done as little modifications as I can. You must get all the grades and take the mean. The problem in your code was that it didn't return any value or append() to the list.

提交回复
热议问题