THE BORE
General => The Superdeep Borehole => Topic started by: Ganhyun on June 03, 2011, 01:43:31 PM
-
So, I decided to go back to school to update some of my skill-sets and got stuck in Python Programming. Ive done ok so far, but now I'm stuck.
Here is the problem I'm stuck on:
Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Write the following functions in the program:
calc_average-- This function should accept five test scores as an arguments and return the average of the scores.
determine_grade-- This function should accept a test score as an argument and return a letter grade for the score, based on the following grading scale:
90-100 A
80-89 B
70-79 C
60-69 D
below 60 F
I get the asking for the user input, thats easy.
That would be along the lines of this:
num1 = input('Please enter your first test score: ')
num2 = input('Please enter your second test score: ')
num3 = input('Please enter your third test score: ')
num4 = input('Please enter your fourth test score: ')
num5 = input('Please enter your fifth test score: ')
# Run the calc_average and determine_grade functions
average = calc_average(num1, num2, num3, num4, num5)
The calc_average is easy too:
def calc_average(num1, num2, num3, num4, num5):
average_score = (num1 + num2 + num3 + num4 + num5) / 5
return average_score
Its the next part that messes me up. Im not sure how to have the determine_grade function work in this setup. Any help would be appreciated.
-
don't know python but it should just be if statements
def determine_grade(average)
if(average >= 90 )
return 'A'
else if (average >= 80 && average <= 89
return 'B'
ect.
-
Thanks for helping, but I have to do that for each grade entered. But likely I'd need to use if elif and else statements for this. I just need to figure out how to do it where the function can do it for all the arguments without error :)
-
Case Borys?
-
no fuckin' idea re: python, but borys means use a switch/case statement (or whatevs the python flavor is):
switch (variable_to_evaluate_here)
{
case <possible_value_of_variable>: do_some_appropriate_shit(); break;
case <possible_value_of_variable>: do_some_appropriate_shit(); break;
...
default: herpa();
}
-
hmm, well, I managed to get it to work with this. It might not be pretty to people who code alot, but hey, it does what I need for the class. :)
# This program will take five entered test scores,
# give them each a letter grade, and then average
# the scores and give that score.
def main():
# Zero out the variables
num1 = 0.0
num2 = 0.0
num3 = 0.0
num4 = 0.0
num5 = 0.0
num1 = determine_grade()
num2 = determine_grade()
num3 = determine_grade()
num4 = determine_grade()
num5 = determine_grade()
# Run the calc_average and determine_grade functions
average = calc_average(num1, num2, num3, num4, num5)
print 'The average score is', average
def calc_average(num1, num2, num3, num4, num5):
average_score = (num1 + num2 + num3 + num4 + num5) / 5
return average_score
def determine_grade():
score = input('Enter your test score: ')
if score < 60:
print "Your grade is an F"
elif score < 70:
print "your grade is a D"
elif score < 80:
print "your grade is a C"
elif score < 90:
print "your grade is a B"
else:
print "your grade is an A"
return score
# Call the main function
main()
-
don't know python but it should just be if statements
def determine_grade(average)
if(average >= 90 )
return 'A'
else if (average >= 80 && average <= 89
return 'B'
ect.
I knew how to do that shit in excel! but totally forgot :fbm
-
:lol excel
-
"case" that bitch
damn, programming has never be cooler 8)
-
Case runs way faster than if,if,if,if, although I'm not sure it matters here.
You could just feed the grades into an array, the avg is equal to array(0) + -> array(array.length-1)/(array.length), presuming arrays are 0 indexed in Python. Then you could feed the grades into another array and read them in paralell
"The grade for user number (loop incrementer) is (gradearray(loop_incrementer))"
blahblah
-
thanks for the help guys :) I turned it in and made an A on it, so im not too worried. Though lists weren't taught so I couldnt use them. I "had" to use functions for this.