Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Heads or Tails question

I'm a beginner and I am trying to create a little heads or tails game. I am trying to customize it so that in the beginning there is an input that asks "How many runs would you like?". With that I would like to enter a number which then performs that many runs/trials of the heads or tails game. Any chance anyone can help?

Here is what I have so far:

import random
runs = int(raw_input("How many runs would you like?     "))


total_heads = 0
total_tails = 0
count = 0

while count < {}.format(runs):

    coin = random.randint(1,2)

    if coin == 1:
        print("Heads")
        total_heads += 1
        count += 1

    elif coin == 2:
        print("Tails")
        total_tails += 1
        count += 1

print("You flipped heads", total_heads, "times")
print("You flipped tails", total_tails, "times")

P.S. I googled it for help and ended up copying someone else's work because it was much more elegant than my first attempt

Thank you!

Mod Edit: Added code markdown in order to make code more readable.

What happens when you test your code. Are your results different from what you desire? Do you get any error messages?

I get:

File "headortails.py", line 9, in <module> while count < {}.format(runs): AttributeError: 'dict' object has no attribute 'format'

1 Answer

There are two issues with the following line:

while count < {}.format(runs):

The format method belongs to strings not dictionaries, and {} within a string acts as a placeholder where the formatted content goes. So I'm assuming the person writing the code simply forgot to include quotes like this:

while count < "{}".format(runs):

However that still results in faulty code, because it leads to comparing and int to a number stored in a string which is not a logical comparison to make. It will result in the code looping infinitely. In reality there is no need to make it a string or format it, just reference runs directly like this:

while count < runs:

That will result in the loop running correctly.

PS: It's worth noting that the code will only run properly in Python 2 (due to the use of raw_input) and not Python 3 which is what you are actually thought on Treehouse. In order to get the code to run in Python 3 you would need to replace raw_input with input.

Thank you andren! Still trying to figure out the basics. I appreciate your detailed explanation :)