Computer Science Canada

[Python] Help with dividing with lists

Author:  BigBear [ Mon Mar 30, 2009 8:16 pm ]
Post subject:  [Python] Help with dividing with lists

Python:
totalweeds = 0
names = []
weeds = []
kids = int(raw_input("How many kids worked?"))
for i in range (0, kids):
    name = raw_input("Enter name.")
    names.append(name)
    numweeds = int(raw_input("Enter how many weeds they pulled: "))
    weeds.append(numweeds)
for i in range (0, kids):
    totalweeds = totalweeds + weeds[i]
for i in range (0, kids):
    print names[i]+" gets "+ str((weeds[i]/totalweeds) * 100)


For some reason I can display them individually and they have a value but I can't seem to divide them and get a value.

Author:  Alexmula [ Mon Mar 30, 2009 8:38 pm ]
Post subject:  RE:[Python] Help with dividing with lists

when you divide by integers it rounds the numbers .. so for example 3/9 would be 0 and 7/3 would be 2. but 3.00/9.00 is 0.33333.

Author:  Tony [ Mon Mar 30, 2009 9:19 pm ]
Post subject:  RE:[Python] Help with dividing with lists

I think 3.0/9 is sufficient.

Author:  McKenzie [ Wed Apr 01, 2009 9:05 pm ]
Post subject:  Re: [Python] Help with dividing with lists

In this case you can't just add .0 to the end of your code. You need to use float. For example if I have two integers x and y use float(x)/y. The other option is, at the top of your program use:
code:
from __future__ import division

This will cause int/int -> float.

Author:  BigBear [ Thu Apr 02, 2009 2:09 pm ]
Post subject:  RE:[Python] Help with dividing with lists

That makes sence!

I guess right now I see it as pointless and more work to make sure things are ints or floats but it will more usefull later


: