grade average program

For the discussion of Perl, Python, Ruby, and PHP and other interpreted languages.

grade average program

Post by ShadowSniper0819 on Thu Dec 31, 2009 2:34 am
([msg=32432]see grade average program[/msg])

I decided to put myself to making a "grade average program". I just started learning python yesterday, but here's what I have so far:
Code: Select all
# Score Averaging

while 1==1:
    print("Welcome to the Grade Averager!")
    print("To average all entered grades, type -1")

    # Get class size
    size = raw_input("How many students: ")

    # Define variables
    total=0
    average=""
    grade=0
   
    # start user input
    while grade !=-1:
            print("Current point total:"),total
            grade = input("Enter grade: ")
            total = total + grade
           
    #average grades
    print("Calculating your average...")
    average = (total/size)
    print("Your grade average is: "), average


I don't know enough yet to figure out how to make it loop a SPECIFIC number of times (each time being the number of students), so instead I left it up to the user to make the cutoff. I tried using a variable "x", but it kept getting screwy, and I finally ended up picking -1 (because how the hell are you going to get a negative score??)
I also could not "figure out" how to assign a variable for average, so I though "" would suffice...

The only problem that I keep hitting is on the line:
Code: Select all
average = (total/size)

keeps giving me problems :|
The python shell comes back saying that with for that line:
"TypeError: unsupported operand type(s) for /: 'int' and 'str' "

Keep in mind, I just started learning python, so I'm sure there's a whole lot "wrong" with what I have so far. Any feedback on the error I'm getting would be greatly appreciated. :)
ShadowSniper0819
New User
New User
 
Posts: 15
Joined: Thu Dec 10, 2009 3:38 pm
Blog: View Blog (0)


Re: School Assignment having trouble Teacher = asshat

Post by faazshift on Thu Dec 31, 2009 3:01 am
([msg=32436]see Re: School Assignment having trouble Teacher = asshat[/msg])

Well your problem is that raw_input() returns a string and python cannot do math with strings. Try converting the input to an int by surrounding the input function with the int function, like: size = int(raw_input("How many students: "))

To make it loop a specific number of times just use a for loop like:
Code: Select all
for i in range(100):
    print "do something"


Rather than the 'while 1==1:' approach, you might consider using booleans. In python the first letters are capitalized, so you would change it to 'while True:'

By 'assign a variable to average', are you talking about just assigning a beginning value to it? If so, just set it equal to '0'.

Also, I would recommend the ipython shell. It can help make learning python easier.

Hope this helps! Good luck!
faazshift
Contributor
Contributor
 
Posts: 516
Joined: Wed Jun 03, 2009 3:55 pm
Location: Riverton, Utah
Blog: View Blog (0)


Re: School Assignment having trouble Teacher = asshat

Post by fashizzlepop on Thu Dec 31, 2009 11:37 pm
([msg=32529]see Re: School Assignment having trouble Teacher = asshat[/msg])

To simplify this code:

1. You don't need to ask how many members there are in the class. Just divide the sum by the amount of scores received.
2. If you are required to ask for the number of students, it would be a good idea to check that you receive the right amount of scores.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: grade average program

Post by tgoe on Tue Jan 05, 2010 10:48 pm
([msg=32898]see Re: grade average program[/msg])

Sorry to complicate things but it's a good idea to make very clear to your users what input you expect and if you aren't explicit results will be different depending on the version of python running your program.

Code: Select all
#!/usr/bin/env python

grades = ["90", "98.6", "77", "80", "77.45"] #aquired via raw_input
gradesi = [int(float(n)) for n in grades]    #treat input as ints
gradesf = [float(n) for n in grades]         #treat input as floats

print("grades: %s" % (str(grades)))
print("gradesi: %s" % (str(sum(gradesi) / len(gradesi))))
print("gradesi/f: %s" % (str(sum(gradesi) / float(len(gradesi)))))
print("gradesf: %s" % (str(sum(gradesf) / len(gradesf))))


Output:

python2.x
Code: Select all
grades: ['90', '98.6', '77', '80', '77.45']
gradesi: 84
gradesi/f: 84.4
gradesf: 84.61


python3.x
Code: Select all
grades: ['90', '98.6', '77', '80', '77.45']
gradesi: 84.4
gradesi/f: 84.4
gradesf: 84.61
User avatar
tgoe
Contributor
Contributor
 
Posts: 638
Joined: Sun Sep 28, 2008 2:33 pm
Location: q3dm7
Blog: View Blog (0)


Re: grade average program

Post by thetan on Wed Jan 06, 2010 12:14 am
([msg=32904]see Re: grade average program[/msg])

which is one of the horrors of working with scripting languages.

Take the following code in PHP for example:
Code: Select all
<?php
var_dump(0x9fa0ff0b);
?>


Output is as follows

PHP Version 5.2.1 (on a 32-bit system):
Code: Select all
int(2147483647)


PHP Version 5.2.3 (32-bit system):
Code: Select all
float(2678128395)


Yes, the PHP Group/Zend decided to change the type of a literal in a minor release, this should never happen ...
Yet this is something that most scripting languages are subject to out of their nature and the Python Software Foundation isn't exempt.
"If art interprets our dreams, the computer executes them in the guise of programs!" - SICP

Image

“If at first, the idea is not absurd, then there is no hope for it” - Albert Einstein
User avatar
thetan
Contributor
Contributor
 
Posts: 657
Joined: Thu Dec 17, 2009 6:58 pm
Location: Various Bay Area Cities, California
Blog: View Blog (0)



Return to Interpreted Languages

Who is online

Users browsing this forum: No registered users and 0 guests