Simon Harvey
Simon Harvey

Reputation: 25

Problems counting from a list of random numbers

I've decide to learn python over the Christmas break, using my Rasp Pi.

I'm running Python 2.7 and for one of the exercises in the book I'm working through, I'm trying to script a coin tossing program that tosses a coin 100 times then prints the outcome of each toss and total number of head and tails.

The program generates the outcome of each toss and stops after 100 turns.

It's the count I'm stuck on.

My code is:

import random

print ("Welcome to the coin toss simulator")
start = raw_input("Would you like to start: ")

if start == "y":

        count = 0

        while count <= 100:
                outcome = random.randint(1, 2)
                count +=1
                if outcome == 1:
                        print("Heads")
                else:
                        print("Tails")

print("You tossed: ", outcome.count(1), " Heads")
print("You tossed: ", outcome.count(2), " Tails")

input("\n\nPress the enter key to exit.")

The error message I get is:

Traceback (most recent call last):
  File "./coin_toss.py", line 23, in <module>
    print("You tossed: ", outcome.count(1), " Heads")
AttributeError: 'int' object has no attribute 'count'

Upvotes: 0

Views: 72

Answers (3)

heinst
heinst

Reputation: 8786

You could use the high-performance Counter data container Python provides to you:

import random
from collections import Counter

print ("Welcome to the coin toss simulator")
start = raw_input("Would you like to start: ")
c = []

if start == "y":

        count = 0

        while count <= 100:
                outcome = random.randint(1, 2)
                count +=1
                if outcome == 1:
                        print("Heads")
                else:
                        print("Tails")
                c.append(outcome)
count = Counter(c)
print("You tossed: ", count[1], " Heads")
print("You tossed: ", count[2], " Tails")

input("\n\nPress the enter key to exit.")

Upvotes: 0

acdr
acdr

Reputation: 4716

The actual error you're getting is simply because random.randint() returns an integer (because what would that even do?). Then, in your print calls at the end, you try to call the count() method of this integer, but integers don't have a count() method.

I'd suggest keeping track of heads and tails separately. E.g.:

if outcome == 1:
    heads_count += 1
else:
    tails_count += 1

Upvotes: 1

TigerhawkT3
TigerhawkT3

Reputation: 49310

outcome is the result of the coin flip. You can't find how many 1s there are in 1; it doesn't make sense. You have to save the result somewhere, such as in a list. Then you can count the occurrences of each number in it:

        outcome = [] # initialize the list
        while count <= 100:
                outcome.append(randint(1, 2)) # add the result to the list
                count +=1
                if outcome[-1] == 1: # check the last element in the list
                        print("Heads")
                else:
                        print("Tails")

print("You tossed: ", outcome.count(1), " Heads") # now these work
print("You tossed: ", outcome.count(2), " Tails")

Upvotes: 0

Related Questions