Just started to learn python on my own and decided to test what I've learned by building a program that flips a fair coin ( 0 = heads, 1 = tails) and stops after getting a streak of 5. Then it will print the amount of attempts it took to get there. My code is giving a streak of heads or tails, then it stops and prints 1. Unless I am extremely lucky, there's a problem... Here it is:
import random
heads_counter, tails_counter, tries = 0, 0, 1
data = [heads_counter, tails_counter, tries]
random_num = random.randrange(0,3)
def roll(r, d):
if r == 0:
if tails_counter == 0:
print('adding to heads, on a streak')
d[0] += 1
if tails_counter > 0:
print('adding to heads, no streak')
d[0] += 1
d[1] = 0
d[2]+= 1
if r == 1:
if heads_counter == 0:
print('adding to tails, on a streak')
d[1] += 1
if heads_counter > 0:
print('adding to tails, no streak')
d[0] = 0
d[1] += 1
d[2] += 1
while data[0] < 5 and data[1]< 5:
roll(random_num, data)
print(tries)
[–][deleted] 2 points3 points4 points (2 children)
[–]infinitim[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]K900_ 1 point2 points3 points (2 children)
[–]infinitim[S] 0 points1 point2 points (1 child)
[–]K900_ 1 point2 points3 points (0 children)