I think you can use pretty much the same list that you used for the simpler problem. All you need to come up with is a formula to calculate the index for each grade.
def main():
grade = raw_input("Enter the student's grade: ")
index = int(grade)/10
grades = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A"]
final = grades[int(index)]
print final
main()
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
The above needs another element at the end of the list so it will handle a perfect score of 100%. I modified the program to test all possible values. It's still not elegant.
def CalcGrade(grade):
"""Converts a numeric grade between 0 and 100 into a letter between 'A' and 'F' """
index = int(grade)/10
#Added another "A" to grades list to handle perfect grade of 100
grades = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A"]
final = grades[int(index)]
return final
#grade = raw_input("Enter the student's grade: ")
# Commented out the raw input and used loop to test all values
for x in range(101): #Test for values between 0 and 100
print x, " Converts to ", CalcGrade(x)
d5e5
Practically a Posting Shark
810 posts since Sep 2009
Reputation Points: 159
Solved Threads: 159
A very simple way:
# create a string where the index gives the grade
grade_str = 'F'*60 + 'D'*10 + 'C'*10 + 'B'*10 + 'A'*11
# ask for grade number input
grade = int(raw_input("Enter the student's grade (0 to 100): "))
# index the result
print "Grade letter =", grade_str[grade]
However, don't hand this in as assignment because your instructor will know it's not yours.
Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
Another way to do it using if statments:
>>> def grades():
grade = int(raw_input("Enter the student's grade: "))
if grade >= 90:
return 'A'
if grade >= 80:
return 'B'
if grade >= 70:
return 'C'
if grade >= 60:
return 'D'
return 'F'
>>> grades()
Enter the student's grade: 55
'F'
>>> grades()
Enter the student's grade: 93
'A'
>>> grades()
Enter the student's grade: 72
'C'
lukerobi
Junior Poster in Training
50 posts since Sep 2009
Reputation Points: 14
Solved Threads: 16
Plenty of good solutions, pick one that fits your level.
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417