Posted: 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 inrange(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 inrange(0, kids):
totalweeds = totalweeds + weeds[i] for i inrange(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.
Sponsor Sponsor
Alexmula
Posted: 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.
Tony
Posted: Mon Mar 30, 2009 9:19 pm Post subject: RE:[Python] Help with dividing with lists
Posted: 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.
BigBear
Posted: 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