Issue
I was trying to write a program that that counts the longest streak of heads in a random 100 coin toss, Able to print print out the toss result but I don't how to initialize the count for longest streak and go about it, Am new to programming and python
import random
total_heads = 0
count = 0
while count < 100:
coin = random.randint(1, 2)
if coin == 1:
print("H")
total_heads += 1
count += 1``
elif coin == 2:
print("T")
Solution
This should do the trick:
import random
total_heads = 0
count = 0
longest_streak = 0
current_streak = 0
while count < 100:
coin = random.randint(1, 2)
if coin == 1:
print("H")
total_heads += 1
current_streak += 1
if current_streak > longest_streak:
longest_streak = current_streak
else:
print("T")
current_streak = 0
count += 1
print(longest_streak)
print(total_heads)
Answered By - Timo Dempwolf
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.