I'm new to python, taking my first class in it right now, only about 4 weeks in.
The assignment is to calculate test average and display the grade for each test inputted.
Part of the assignment is to use a function to calculate the average as well as deciding what letter grade to be assigned to each score.
As I understand it, functions are supposed to help cut down global variables.
My question is this: how do I condense this code?
I don't know how to use a function for deciding letter grade and then displaying that without creating a global variable for each grade that has been inputted.
If you notice any redundancy in my code, I would appreciate a heads up and a little lesson on how to cut that out. I can already smell the mark downs I will get if I turn this in as is...
def main():
grade1=float(input( "Enter score (0-100):"))
while (grade1 <0 or grade1 >100 ):
if grade1 <0 or grade1 >100:
print("Please enter a valid grade")
grade1=float(input( "Enter score (0-100):"))
grade2=float(input( "Enter score (0-100):"))
while (grade2 <0 or grade2 >100 ):
if grade2 <0 or grade2 >100:
print("Please enter a valid grade")
grade2=float(input( "Enter score (0-100):"))
grade3=float(input( "Enter score (0-100):"))
while (grade3 <0 or grade3 >100 ):
if grade3 <0 or grade3 >100:
print("Please enter a valid grade")
grade3=float(input( "Enter score (0-100):"))
grade4=float(input( "Enter score (0-100):"))
while (grade4 <0 or grade4 >100 ):
if grade4 <0 or grade4 >100:
print("Please enter a valid grade")
grade4=float(input( "Enter score (0-100):"))
grade5=float(input( "Enter score (0-100):"))
while (grade5 <0 or grade5 >100 ):
if grade5 <0 or grade5 >100:
print("Please enter a valid grade")
grade5=float(input( "Enter score (0-100):"))
total=grade1+grade2+grade3+grade4+grade5
testAverage=calcAverage(total)
eachGrade1=determineGrade(grade1)
eachGrade2=determineGrade(grade2)
eachGrade3=determineGrade(grade3)
eachGrade4=determineGrade(grade4)
eachGrade5=determineGrade(grade5)
print("\nTest #1 grade:", (eachGrade1))
print("Test #2 grade:", (eachGrade2))
print("Test #3 grade:", (eachGrade3))
print("Test #4 grade:", (eachGrade4))
print("Test #5 grade:", (eachGrade5))
print("\nTest average:", (testAverage),("%"))
def calcAverage(total):
average=total/5
return average
def determineGrade(grade):
if grade >=90:
return "A"
elif grade >=80:
return "B"
elif grade >=70:
return "C"
elif grade >=60:
return "D"
else:
return "F"