943,608 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 3431
  • Python RSS
Sep 28th, 2009
0

A grade input program

Expand Post »
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.

Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
A_Dubbs is offline Offline
26 posts
since Sep 2009
Sep 28th, 2009
0

Re: A grade input program

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.
PYTHON Syntax (Toggle Plain Text)
  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()
Reputation Points: 153
Solved Threads: 141
Master Poster
d5e5 is offline Offline
736 posts
since Sep 2009
Sep 29th, 2009
0

Re: A grade input program

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.
PYTHON Syntax (Toggle Plain Text)
  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)
Reputation Points: 153
Solved Threads: 141
Master Poster
d5e5 is offline Offline
736 posts
since Sep 2009
Sep 29th, 2009
0

Re: A grade input program

A very simple way:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 30th, 2009
0

Re: A grade input program

Another way to do it using if statments:

Python Syntax (Toggle Plain Text)
  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.  
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 1st, 2009
0

Re: A grade input program

Plenty of good solutions, pick one that fits your level.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 5th, 2009
0

omg

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bmmahone is offline Offline
1 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python help
Next Thread in Python Forum Timeline: How to create a real-time numeric display in python?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC