This code is meant to do the following:
- Take a value for the number of classes a user is taking
- Add grades into a list with the same length as the previously specified classes
- Find the average of those values in the list
- Output a letter grade equivalent based on that average
This is an assignment for school I'm working on and as a novice user of Python, I wanted some help in trying to figure out where I'm going wrong. When I run this code, I get the following error: "Line 13: TypeError: '>=' not supported between instances of 'list' and 'int'". I'm super curious why I'm getting that error because I don't think I'm passing average over to numToLetterGrade as a list.
I've tried a couple of things involving using a while loop to iterate through each part of the list and add it that way instead of using sum and len but got the same result. I'm also in a situation where I keep seeing the use of for loops, but I haven't officially learned that in class yet and my professor doesn't want me to use code that I haven't used.
Any ideas would be greatly appreciated. Thanks!
def getUserGrades(grades):
inputsList = list(grades)
return inputsList
def calNumAverage(inputsList):
gradeAverage = 0
average = sum(inputsList)/len(inputsList)
return average
def numToLetterGrade(average):
letterGrade = " "
if(average >= 93 and average <= 100):
letterGrade = "A "
elif(average >= 90 and average <= 92):
letterGrade = "A"
elif(average >= 87 and average <= 89):
letterGrade = "A-"
elif(average >= 83 and average <= 86):
letterGrade = "B "
elif(average >= 80 and average <= 82):
letterGrade = "B"
elif(average >= 70 and average <= 79):
letterGrade = "C"
elif(average >= 60 and average <= 69):
letterGrade = "D"
else:
letterGrade = "F"
return letterGrade
def main():
grades = []
user1 = int(input("Number of classes :"))
while(user1 != 0):
grades.append(float(input("Numeric Grade: ")))
user1 = user1 - 1
lists = getUserGrades(grades)
average1 = numToLetterGrade(lists)
ltrGrade = average1(numToLetterGrade)
print("Your average letter grade is: " ltrGrade)
main()
Example Expected Output:
- Number of Classes: 3
- Numeric Grade: 85
- Numeric Grade: 90
- Numeric Grade: 80
- Your average letter grade is: B
CodePudding user response:
You actually got the code construct and the solution algorithms right. But you missed some things in the main function, compare the changes here -
def main():
grades = []
user1 = int(input("Number of classes :"))
while(user1 != 0):
grades.append(float(input("Numeric Grade: ")))
user1 = user1 - 1
lists = getUserGrades(grades)
average1 = calNumAverage(lists)
ltrGrade = numToLetterGrade(average1)
print("Your average letter grade is: " ltrGrade)
CodePudding user response:
Here we see you are passing the return value of getUserGrades
, a list, to numToLetterGrade
, which is expecting an integer value.
lists = getUserGrades(grades)
average1 = numToLetterGrade(lists)
ltrGrade = average1(numToLetterGrade)
You are also trying to call average1
after this as if it were a function, and passing it the numToLetterGrade
function as an argument.
Note, getUserGrades
is completely superfluous, since grades
is already a list.
This example should give you the correct results:
def calNumAverage(inputsList):
average = sum(inputsList)/len(inputsList)
return average
def numToLetterGrade(average):
letterGrade = " "
if(average >= 93 and average <= 100):
letterGrade = "A "
elif(average >= 90 and average <= 92):
letterGrade = "A"
elif(average >= 87 and average <= 89):
letterGrade = "A-"
elif(average >= 83 and average <= 86):
letterGrade = "B "
elif(average >= 80 and average <= 82):
letterGrade = "B"
elif(average >= 70 and average <= 79):
letterGrade = "C"
elif(average >= 60 and average <= 69):
letterGrade = "D"
else:
letterGrade = "F"
return letterGrade
def main():
grades = []
user1 = int(input("Number of classes :"))
while(user1 != 0):
grades.append(float(input("Numeric Grade: ")))
user1 = user1 - 1
avg = calNumAverage(grades)
grade = numToLetterGrade(avg)
print("Your average letter grade is: " grade)
main()
Cleaned up further:
def average(lst):
return sum(lst) / len(lst)
def letter_grade(score):
if 93 <= score <= 100: return "A "
elif 90 <= score <= 92: return "A"
elif 87 <= score <= 89: return "A-"
elif 83 <= score <= 86: return "B "
elif 80 <= score <= 82: return "B"
elif 70 <= score <= 79: return "C"
elif 60 <= score <= 69: return "D"
return "F"
if __name__ == "__main__":
classes = int(input("Number of classes: "))
grades = [float(input("Numeric Grade: ")) for _ in range(classes)]
print("Your average letter grade is: " letter_grade(average(grades)))