so I am new to python and I am trying to generate a 100 numbers from 0 to 10 and find out how many times a three appears. I'm not sure exactly how to get it to work. I basically just want to be able to count how many times a 3

import random
count = 0
for x in range (100)
    i = random.randint(1,10)
    print random.randint(1,10)
    if  i == 3:
        count = count + 1
        print count

i am hoping count represents the number of time 3 appears. Im guesing one of the problems is setting i = random. thanks for any input

thanks but I think i got it. I think my algorithm and code is correct I don't know why it wasn't working befor but it is now. If anyone still wants to comment feel free. I m always open to constructive criticism. thanks ef

I only see one problem. That is where you print the random number. IE, you do this:

    i = random.randint(1,10)
    print random.randint(1,10)

but you need to do this, because the second line with the print statement will get a new random number:

    i = random.randint(1,10)
    print i

So in your case, the print statement will not reflect the actual number assigned to i. :-)

Comments
Thanks that makes sense. I know I wouldn't have caught that.

Can clean it up little.

import random

count = 0
for x in range (100):
    i = random.randint(1,10)
    if i == 3:
        count = count + 1    
print count

There are other way to write this,here is one.

>>> import random
>>> [random.randint(1,10) for i in range(100)].count(3)
7

Edited 3 Years Ago by snippsat

Just to leave no stone unturned:

import random

mylist = []
for x in range(100):
    if random.randint(1,10) == 3:
        mylist.append(3)    

print(len(mylist))
Comments
Yeah I thought of this method and have used it in different problems but I wasn't too sure If it would works for this. So thank you I'll give it a try

One more stone turned:

import collections
import random

# values are integers
ddict = collections.defaultdict(int)
for k in range(100):
    n = random.randint(0, 10)
    ddict[n] += 1

print(ddict[3])

One more stone turned:

Collections is good,and i think we should also mention the star when it's come to counting stuff collections.Counter.

>>> from collections import Counter
>>> import random
>>> Counter(random.randint(1,10) for i in range(100))[3]
8
>>> Counter(random.randint(1,10) for i in range(100))[3]
9

Some more fun with Counter.

>>> from collections import Counter
>>> lst = [3,3,3,3,9,9,1,3,3,5]
>>> counter = Counter(lst)
>>> counter[3]
6
>>> counter.most_common(2)
[(3, 6), (9, 2)]
>>> counter.most_common(1)
[(3, 6)]


>>> fruits = ['Apple', 'Apple', 'Pear', 'Orange', 'Banana', 'Apple', 'Banana']
>>> fruit_count = Counter(fruits)
>>> fruit_count
Counter({'Apple': 3, 'Banana': 2, 'Orange': 1, 'Pear': 1})
>>> fruit_count.most_common(1)
[('Apple', 3)]
>>> fruit_count.most_common(2)
[('Apple', 3), ('Banana', 2)]
>>> fruit_count['Apple']
3


>>> import itertools
>>> nested_lst = [[1,3,3],[1,2,3]]
>>> nested_counter = Counter(itertools.chain(*nested_lst))
>>> nested_counter[3]
3
>>> nested_counter
Counter({3: 3, 1: 2, 2: 1})
>>> nested_counter.most_common(1)
[(3, 3)]

Edited 3 Years Ago by snippsat

Comments
nice
This question has already been answered. Start a new discussion instead.