I am writing a program that uses two functions to get a letter grade and get a score average. I am stuck on what I have down below and have been researching for longer than I should have for this assignment. The two functions that need to be used is calcAverage(grade1, grade2, grade3, grade4, grade5)
and gradeLetter(grade)
. I figured out how to get the average but stuck on gradeLetter
function.
def main():
grade1 = int(input("Please enter your first grade: "))
grade2 = int(input("Please enter your second grade: "))
grade3 = int(input("Please enter your third grade: "))
grade4 = int(input("Please enter your fourth grade: "))
grade5 = int(input("Please enter your fifth grade: "))
avg=calcAverage(grade1, grade2, grade3, grade4, grade5)
print(avg)
def gradeLetter(grade):
if grade > 90 or grade <= 100:
return 'A'
elif grade > 80 or grade < 90:
return 'B'
elif grade > 70 or grade < 80:
return 'C'
elif grade > 60 or grade < 70:
return 'D'
elif grade < 60:
return 'F'
def calcAverage(grade1, grade2, grade3, grade4, grade5):
avg = (grade1 grade2 grade3 grade4 grade5) / 5
return avg
main()
CodePudding user response:
(I can't comment due to lack of reputation :/)
You need to change the logic statement to and
:
if grade > 90 and grade <= 100:
on another note, this is even simpler:
if 90 < grade <= 100:
CodePudding user response:
gradeLetter
will always return "A" the way you've written it becausegrade <= 100
will always be True (with meaningful inputs).- You can condense
gradeLetter
to check only one side of the inequality since you're usingelif
. - Finally, simply call the function in
main
after you print the average:
def main():
grade1 = int(input("Please enter your first grade: "))
grade2 = int(input("Please enter your second grade: "))
grade3 = int(input("Please enter your third grade: "))
grade4 = int(input("Please enter your fourth grade: "))
grade5 = int(input("Please enter your fifth grade: "))
avg=calcAverage(grade1, grade2, grade3, grade4, grade5)
print(avg)
print(gradeLetter(avg)) #add this line
def gradeLetter(grade):
if grade >= 90: #modify your conditional statements
return 'A'
elif grade >= 80:
return 'B'
elif grade >= 70:
return 'C'
elif grade >= 60:
return 'D'
else:
return 'F'
CodePudding user response:
You need to call the gradeLetter function inside main()
as
avg = calcAverage(grade1, grade2, grade3, grade4, grade5)
print(gradeLetter(avg))
Also gradeLetter
method has wrong conditions to compare for.
Modify function as
def gradeLetter(grade):
if grade > 90 and grade <= 100:
return 'A'
elif grade > 80 and grade < 90:
return 'B'
elif grade > 70 and grade < 80:
return 'C'
elif grade > 60 and grade < 70:
return 'D'
elif grade < 60:
return 'F'
Else your code with or
conditions will fail for this output:
Please enter your first grade: 1
Please enter your second grade: 2
Please enter your third grade: 3
Please enter your fourth grade: 4
Please enter your fifth grade: 5
3.0
A