Steven D Andrews
Steven D Andrews

Reputation: 115

Starting out in Python Development: Coin-Tossing Loops

Afternoon all,

I cross your paths as someone looking to teachimself programming. As such, I've started with Python. As a disclaimer, I have searched the question for some examples of Python coin-tosses but I've not really understood any of the code that previous askers have come up with.

My task My educationanal material has asked me to come up with an application that flips the virtual coin 100 times and then prints the results. My ideas was to use an infinite loop, break out when the coin toss reaches 100 and then print the results.

I've not quite achieved this and I'm not spotting the error in what I've written. It seems to achieve the 100 flips but then only prints out 50 of either Heads / Tails; thus my error is somewhere in the counting logic?

Any explanation (bearing in mind I'm a beginner, not a moron!) would get both my gratitude and my upvote

Thanks for reading

# Exercise 3.2
# Heads and Tails coin flip

#import random
import random

#declare variables
heads = 0
tails = 0
cointoss = 0
coinresult = random.randint(1,2)

#start the loop
while True:
    cointoss +=1

    #end the loop if cointoss is greater than 100
    if cointoss > 100:
        break
    if coinresult == 1:
        heads +=1
        cointoss +=1
    elif coinresult == 2:
        tails +=1
        cointoss +=1

  print("Heads came up", heads, "times")
  print("Tails came up", tails, "times")

Upvotes: 0

Views: 3136

Answers (5)

Paul Collingwood
Paul Collingwood

Reputation: 9116

Put this line:

coinresult = random.randint(1,2)

inside the while loop. Otherwise you get value once, and just use it over and over inside the loop and you were adding to cointoss in two places per loop.

Upvotes: 3

Abhijit
Abhijit

Reputation: 63707

If you want to be adventurous, look into generator expressions and the built-in sum along with random.randint

heads = sum(random.randint(0,1) for _ in range(100))
print("Heads came up {} times".format(heads))
print("Tails came up {} times".format(100 - heads))

Upvotes: 0

Gyhth
Gyhth

Reputation: 1165

# Exercise 3.2
# Heads and Tails coin flip

#import random
import random

#declare variables
heads = 0
tails = 0
cointoss = 0
coinresult = random.randint(1,2)

#start the loop
while True:
   cointoss +=1

#end the loop if cointoss is greater than 100
   if cointoss > 100:
       break
   if coinresult == 1:
       heads +=1
   elif coinresult == 2:
       tails +=1
   coinresult = random.randint(1,2)

print("Heads came up", heads, "times")
print("Tails came up", tails, "times")

You never recalled your randomization, therefore the number never changed, and you were adding to cointoss in two places per loop.

Upvotes: 1

girasquid
girasquid

Reputation: 15516

You might have an easier time writing your loop by using the range method, like this:

for i in range(0, 100):
    coinresult = random.randint(1, 2)
    if coinresult == 1:
        heads += 1
    else:
        tails += 1
print("Heads came up", heads, "times")
print("Tails came up", tails, "times")

Upvotes: 3

Alex Hammel
Alex Hammel

Reputation: 335

You're incrementing cointoss twice per loop.

while True:
    cointoss +=1      # You already incremented here, therefore...
    if cointoss > 100:
        break
    if coinresult == 1:
        heads +=1
        cointoss +=1  # ...get rid of this...
    elif coinresult == 2:
        tails +=1
        cointoss +=1  # ...and this.

Also, as was pointed out, you should be getting a new coinresult somewhere inside the loop.

Upvotes: 4

Related Questions