[Tutor] Summing up numbers in a file

2010-10-26 Thread masawudu bature
How do I sum all occurrences of  a number in a file?
I have a file with a bunch of numbers, each on it's own line:

5
3
11
3
7
3
5
5
11
7
7
...
How do i sum them up so that the output will be ,
5 :  15
3 :   9
11:  22
7 :   21


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python Help

2010-09-27 Thread masawudu bature
Thanks David, But the loop was suppose to produce the count of even divisors an 
integer has.
Like, 6 has 2 "even" divisors, which is 2 and 6, itself.
8 = 3 even divisors, which is 2, 4 ,and 8
10 = 2 even divisors, which is 2, and 10
12 = 4 even divisors, which is 2, 4, 6, and 12

sorry, I just don't know how to write a code to count the number of divisors





From: David Hutto 
To: masawudu bature 
Cc: tutor@python.org
Sent: Mon, September 27, 2010 11:36:05 PM
Subject: Re: [Tutor] Python Help

On Tue, Sep 28, 2010 at 12:15 AM, masawudu bature  wrote:
> I'm having a hard time finding the count of divisors that are even. Help
> anybody?
>
> Here's my code.
>
> The output is suppose to count the number of even divisors the range has.
>
> def evenCount(b) :
> for n in range(x, y+1) :
> count = 0
> if n % 2 == 0 :
> count += n/3
> count % n == 1
> return n
>
>
> ### main 
>
> x = input("Enter a starting value: ")
> y = input("Enter a stopping value: ")
>
> count = evenCount(x)
>
> for n in range(x, y+1) :
> count = 0
> if n % 2 == 0 :
> count += n/3
> print "%2d: " %n, "has%2d" %count, "even divisors",
> else :
> print "%2d: " %n, "has 0 even divisors",
> print
>
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

#You start with your range variables
x = input("Enter a starting value: ")
y = input("Enter a stopping value: ")

#You set a for loop for range
for num in range(x, y):
#If that number divided by 2 has a remainder of 0, it's of course even.
if num % 2 == 0:
#print the number
print num

And various other ways as well.

David



  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python Help

2010-09-27 Thread masawudu bature
I'm having a hard time finding the count of divisors that are even. Help 
anybody?

Here's my code.

The output is suppose to count the number of even divisors the range has.

def evenCount(b) :
for n in range(x, y+1) :
count = 0
if n % 2 == 0 :
count += n/3
count % n == 1
return n


### main 

x = input("Enter a starting value: ")
y = input("Enter a stopping value: ")

count = evenCount(x)

for n in range(x, y+1) :
count = 0
if n % 2 == 0 :
count += n/3
print "%2d: " %n, "has%2d" %count, "even divisors",
else :
print "%2d: " %n, "has 0 even divisors",
print


  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor