A grade input program

Thread Solved

Join Date: Sep 2009
Posts: 12
Reputation: A_Dubbs is an unknown quantity at this point 
Solved Threads: 0
A_Dubbs A_Dubbs is offline Offline
Newbie Poster

A grade input program

 
0
  #1
Sep 28th, 2009
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.

  1. def main():
  2. grade = raw_input("Enter the student's grade: ")
  3. grades = ["F", "F", "D", "C", "B", "A"]
  4. final = grades[int(grade)]
  5. print final
  6.  
  7. 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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster

Re: A grade input program

 
0
  #2
Sep 28th, 2009
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.
  1. def main():
  2. grade = raw_input("Enter the student's grade: ")
  3. index = int(grade)/10
  4. grades = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A"]
  5. final = grades[int(index)]
  6. print final
  7.  
  8. main()
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: d5e5 is an unknown quantity at this point 
Solved Threads: 6
d5e5 d5e5 is offline Offline
Light Poster

Re: A grade input program

 
0
  #3
Sep 29th, 2009
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.
  1. def CalcGrade(grade):
  2. """Converts a numeric grade between 0 and 100 into a letter between 'A' and 'F' """
  3. index = int(grade)/10
  4.  
  5. #Added another "A" to grades list to handle perfect grade of 100
  6. grades = ["F", "F", "F", "F", "F", "F", "D", "C", "B", "A", "A"]
  7. final = grades[int(index)]
  8. return final
  9.  
  10. #grade = raw_input("Enter the student's grade: ")
  11. # Commented out the raw input and used loop to test all values
  12. for x in range(101): #Test for values between 0 and 100
  13. print x, " Converts to ", CalcGrade(x)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,542
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 172
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: A grade input program

 
0
  #4
Sep 29th, 2009
A very simple way:
  1. # create a string where the index gives the grade
  2. grade_str = 'F'*60 + 'D'*10 + 'C'*10 + 'B'*10 + 'A'*11
  3. # ask for grade number input
  4. grade = int(raw_input("Enter the student's grade (0 to 100): "))
  5. # index the result
  6. print "Grade letter =", grade_str[grade]
However, don't hand this in as assignment because your instructor will know it's not yours.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training

Re: A grade input program

 
0
  #5
Sep 30th, 2009
Another way to do it using if statments:

  1. >>> def grades():
  2. grade = int(raw_input("Enter the student's grade: "))
  3. if grade >= 90:
  4. return 'A'
  5. if grade >= 80:
  6. return 'B'
  7. if grade >= 70:
  8. return 'C'
  9. if grade >= 60:
  10. return 'D'
  11. return 'F'
  12.  
  13. >>> grades()
  14. Enter the student's grade: 55
  15. 'F'
  16. >>> grades()
  17. Enter the student's grade: 93
  18. 'A'
  19. >>> grades()
  20. Enter the student's grade: 72
  21. 'C'
  22.  
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,019
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 931
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: A grade input program

 
0
  #6
Oct 1st, 2009
Plenty of good solutions, pick one that fits your level.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: bmmahone is an unknown quantity at this point 
Solved Threads: 0
bmmahone bmmahone is offline Offline
Newbie Poster

omg

 
0
  #7
Oct 5th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC