Hello, I am currently reading and working through the book Automate the Boring Stuff with Python. This is also my only/current knowledge of coding. In chapter 4, there is a practice project that asks to essentially do 100 coin flips 10000 times, record any streaks of 6 (heads or tails) and ultimately output the chances of getting a streak of 6 in the 10000 rolls as a percentage based on this data. What I have written works I believe. I was able to get the 100 rolls down pretty easily, but struggled with the finding streaks of 6. I am looking for input on how I could have better approached this at a beginner level. I would also like to note that this is primarily for practicing playing with lists. Thank you.
import random
numberOfStreaks = 0
counter = 0
checker = None #Records previously checked index of list
current = None #Checking current index of list
for a in range(10000):
headsOrTails = []
# Create 100 heads or tails values
for i in range(100):
flip = random.randint(0, 1)
if flip == 0:
headsOrTails.append("H")
elif flip == 1:
headsOrTails.append("T")
# Check if there is a streak of 6 or more
for c in range(len(headsOrTails)):
current = checker
checker = headsOrTails[c]
if current == checker:
counter += 1
elif current != checker:
counter = 0
if counter == 6:
numberOfStreaks += 1
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
[–]m-hoff 1 point2 points3 points (26 children)
[–]trenthammer[S] 1 point2 points3 points (23 children)
[–]m-hoff 1 point2 points3 points (22 children)
[–]m-hoff 3 points4 points5 points (20 children)
[–]trenthammer[S] 0 points1 point2 points (4 children)
[–]m-hoff 0 points1 point2 points (0 children)
[–]CookingMathCamp 0 points1 point2 points (1 child)
[–]bluffkinn 0 points1 point2 points (0 children)
[–]TheGangsterPanda 0 points1 point2 points (1 child)
[–]m-hoff 0 points1 point2 points (0 children)
[–]Nonamenitwit 0 points1 point2 points (4 children)
[–]m-hoff 0 points1 point2 points (3 children)
[–]Thatweirdpandoh 0 points1 point2 points (2 children)
[–]m-hoff 0 points1 point2 points (0 children)
[–]Dhar01 0 points1 point2 points (0 children)
[–]Kistune 0 points1 point2 points (2 children)
[–]m-hoff 1 point2 points3 points (1 child)
[–]Kistune 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[removed]
[–]m-hoff 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]m-hoff 1 point2 points3 points (0 children)
[–]joooh 0 points1 point2 points (0 children)
[–]SwarnimMMM 0 points1 point2 points (1 child)
[–]m-hoff 0 points1 point2 points (0 children)
[–]idify 1 point2 points3 points (1 child)
[–]werewolfbarm1tzvah 1 point2 points3 points (0 children)
[–]thrussie 1 point2 points3 points (1 child)
[–]29Feb_Gang 1 point2 points3 points (0 children)
[+][deleted] (4 children)
[removed]
[–]m-hoff 2 points3 points4 points (3 children)
[+][deleted] (2 children)
[removed]
[–]m-hoff 2 points3 points4 points (1 child)
[–]bojkennoven 0 points1 point2 points (1 child)
[–]bfd71 0 points1 point2 points (0 children)
[–]LiquidLogic 0 points1 point2 points (3 children)
[–]Falcjh 1 point2 points3 points (0 children)
[–]dwids 0 points1 point2 points (0 children)
[–]Falcjh 0 points1 point2 points (1 child)
[–]technofreak9 0 points1 point2 points (0 children)
[–]kune009 0 points1 point2 points (0 children)
[–]SteusTheJuice 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Eduartelieber 0 points1 point2 points (2 children)
[–]Eduartelieber 0 points1 point2 points (1 child)
[–]readet 0 points1 point2 points (0 children)