Python Forum
Random module, coin flipping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random module, coin flipping
#1
Hey guys,

this exercise is confusing me: The idea here is to create a program, which simulates coin flips by randomly selecting 0 (Tails) or 1 (Heads) and printing out the result. When working correctly, the program prints out something like this:
11

Obviously, as the program applies random activities, it may give any combination of five heads or tails. For example, running the program a second time resulted in this: 22


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

this is my code:
import random
 
numbers = []
#picks 5 random numbers from 0 to 1
while True:
    if len(numbers) == 5:
        break
    pick = random.randint(0,1)
    numbers.append(pick)

#0 == Tails; 1 == Heads
for i in numbers:
	if pick == 0:
		print("Tails!")
	else:
		print("Heads!")
and this is the error I'm getting:
33


It seems to me that the first automatic input is supposed to generate only one flip, and then for the second time, it generates 5, but I don't know how to do that. Perhaps it's actually supposed to always generate 5 and I'm doing something wrong in my code.

Thanks in advance! Cool
Reply
#2
This is not really a problem with your code. Your code works, it just doesn't seem to satisfy the requirements of the exercise. Without seeing the exercise as written by the professor, I can't really tell you what to do.

I would suggest trying to make the program work with only one loop. Generate the random number and print heads or tails all in the same loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
I guess you should be using "i" instead of "pick" in if block condition,

import random
  
numbers = []
#picks 5 random numbers from 0 to 1
while True:
    if len(numbers) == 5:
        break
    pick = random.randint(0,1)
    #print(pick)
    numbers.append(pick)

print(numbers)
#0 == Tails; 1 == Heads
for i in numbers:
    if i == 0:
        print("Tails!")
    else:
        print("Heads!")
Output:
python test.py [0, 0, 1, 0, 1] Tails! Tails! Heads! Tails! Heads!
Best Regards,
Sandeep

GANGA SANDEEP KUMAR
Reply
#4
This is homework and following may not apply.

To simulate coin flipping one can just:

>>> random.choices(['heads', 'tails'], k=5)
['tails', 'tails', 'heads', 'tails', 'tails']
To verify that it its evenly distributed:

>>> random.choices(['heads', 'tails'], k=1000).count('heads')        
501
One can add weights or cumulative weights ('biased coin'):

>>> random.choices(['heads', 'tails'], cum_weights=[0.70, 1.00], k=1000).count('heads')                                               
703
Result can be printed out using unpacking and sep:

>>> print(*random.choices(['heads', 'tails'], k=5), sep='\n') 
tails
heads
heads
tails
heads
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the frequency of each coin for the combinations that sum to the amount N Pranav 3 2,543 May-19-2020, 06:16 AM
Last Post: Pranav
  Need help with coin change program Gateux 2 6,037 Jun-25-2019, 02:32 PM
Last Post: perfringo
  Adding and Removing coins to match Coin Bag Total infinite times Strayfe 8 4,621 Sep-11-2018, 07:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020