I am doing my homework and am having trouble on a certain problem.

4) A certain CS professor gives 100-point exams that are graded on the scale 90-100:A, 80-89:B, 70-79:C, 60-69:'D, <60:F. Write a program that accepts an exam score as input and prints out the corresponding grade.

We have not learned any kind of an "if" statement in my class yet. I did a problem before this one that was similar:

3) A certain CS professor gives 5-point quizzes that are graded on the scale 5-A, 4-B, 3-C, 2-D, 1-F, 0-F. Write a program that accepts a quiz score as an input and prints out the corresponding grade.

For this, I just used a list.

``````def main():
grade = raw_input("Enter the student's grade: ")
grades = ["F", "F", "D", "C", "B", "A"]
final = grades[int(grade)]
print final

main()``````

I know I could do simultaneous assignment for each number through 100, but I know there is a better / easier way. I think I need to use a loop (we know the for loop) somehow or go back to the list. Also, I believe I would need to incorporate range(). I am drawing a blank. How can I go about solving my problem without using any kind of if statements?

## Recommended Answers

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 …``````

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 …``````

## All 8 Replies

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()``````

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)``````

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.

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'``````

Plenty of good solutions, pick one that fits your level.

omg are you in prof rivera's class??? I'm currently a student with him and we have the exact same problem (hmwk 2) it's sooo hard and i was searching for some help as well...if you're not in the class your question really helped me out. so thanks :)

please show me the code of creating a program that will ask to input a test score form the user and return the corresponding grade value.
An "A" for a score of 90%-100%, "B" for a score of 80%-89% until F..
use if-else-if
THANK YOU ^^

lukerobi's code would be a good start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.