Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Python] Help with dividing with lists
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BigBear




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Alexmula




PostPosted: 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




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

I think 3.0/9 is sufficient.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
McKenzie




PostPosted: 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




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: