Author Topic: Programming Bore, give me a hand  (Read 4139 times)

0 Members and 1 Guest are viewing this topic.

Ganhyun

  • Used to worship Muckhole. Now worships Robo.
  • Senior Member
Programming Bore, give me a hand
« 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.
XDF

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #1 on: June 03, 2011, 01:53:39 PM »
don't know python but it should just be if statements

Code: [Select]
def determine_grade(average)
   if(average >= 90 )
       return 'A'
else if (average >= 80 && average <= 89
      return 'B'

ect.

Ganhyun

  • Used to worship Muckhole. Now worships Robo.
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #2 on: June 03, 2011, 01:58:28 PM »
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 :)
« Last Edit: June 03, 2011, 02:00:58 PM by Ganhyun »
XDF

Ganhyun

  • Used to worship Muckhole. Now worships Robo.
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #3 on: June 03, 2011, 02:49:55 PM »
Case Borys?
XDF

Van Cruncheon

  • live mas or die trying
  • Banned
Re: Programming Bore, give me a hand
« Reply #4 on: June 03, 2011, 03:01:30 PM »
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();
}
duc

Ganhyun

  • Used to worship Muckhole. Now worships Robo.
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #5 on: June 03, 2011, 03:26:12 PM »
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. :)

Code: [Select]
# 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()
XDF

Diunx

  • Humble motherfucker with a big-ass dick
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #6 on: June 03, 2011, 03:37:39 PM »
don't know python but it should just be if statements

Code: [Select]
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
Drunk

Madrun Badrun

  • twin-anused mascot
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #7 on: June 03, 2011, 04:04:22 PM »
:lol excel

Tieno

  • Iconz
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #8 on: June 05, 2011, 01:24:47 AM »
"case" that bitch
damn, programming has never be cooler 8)
i

GilloD

  • TAKE THE LIFE OF FRED ASTAIRE. MAKE HIM PAY. TRANSFER HIS FAME TO YOU.
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #9 on: June 08, 2011, 01:45:10 AM »
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
wha

Ganhyun

  • Used to worship Muckhole. Now worships Robo.
  • Senior Member
Re: Programming Bore, give me a hand
« Reply #10 on: June 08, 2011, 03:05:45 PM »
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.
XDF