Random module, coin flipping - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Random module, coin flipping (/thread-23558.html) |
Random module, coin flipping - gus17 - Jan-05-2020 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: 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: ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 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: 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! RE: Random module, coin flipping - ichabod801 - Jan-05-2020 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. RE: Random module, coin flipping - sandeep_ganga - Jan-06-2020 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!") Best Regards,Sandeep GANGA SANDEEP KUMAR RE: Random module, coin flipping - perfringo - Jan-06-2020 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') 501One can add weights or cumulative weights ('biased coin'): >>> random.choices(['heads', 'tails'], cum_weights=[0.70, 1.00], k=1000).count('heads') 703Result can be printed out using unpacking and sep: >>> print(*random.choices(['heads', 'tails'], k=5), sep='\n') tails heads heads tails heads |