954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

A grade input program

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?

A_Dubbs
Light Poster
26 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

bmmahone
Newbie Poster
1 post since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You