| | |
A grade input program
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 12
Reputation:
Solved Threads: 0
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.
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?
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)
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?
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
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)
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()
•
•
Join Date: Sep 2009
Posts: 40
Reputation:
Solved Threads: 6
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)
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:
However, don't hand this in as assignment because your instructor will know it's not yours.
python Syntax (Toggle Plain Text)
# 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]
drink her pretty
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
Another way to do it using if statments:
Python Syntax (Toggle Plain Text)
>>> 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'
![]() |
Similar Threads
- student grade program [help!] (C++)
- Grade book program using arrays and Bubble sort (C++)
- Grade program (C)
- Help with a program (C++)
- program to simulate a soft drink machine. PLease help!!! (C++)
- grade prediction program (C)
- Clearing input (C)
- Need help writing a program (C++)
Other Threads in the Python Forum
- Previous Thread: python help
- Next Thread: How to create a real-time numeric display in python?
| Thread Tools | Search this Thread |
alarm app beginner cipher cmd cx-freeze data decimals development dictionary directory dynamic error examples feet file float format function generator getvalue gui halp homework http images import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pymailer pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation split sqlite ssh string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wx.wizard wxpython xlwt






