Write a program that simulates rolling a 6-sided die until you get 3 odd numbers in a row.
Sample run
Let’s roll some dice!
You rolled a 3
You rolled a 5
You rolled a 4
You rolled a 1
You rolled a 6
You rolled a 1
You rolled a 4
You rolled a 1
You rolled a 3
You rolled a 5
Three in a row in 10 rolls.
from random import randint
n = randint(1,6)
cnt = 0
foo = 0
if n%2==1: cnt += 1
print("Let's roll some dice!")
print(f"You rolled a {n}")
while True:
foo += 1
if cnt == 3:
print(f"Three in a row in {foo} rolls")
break
n = randint(1,6)
print(f"You rolled a {n}")
if n%2==1:
cnt += 1
else:
cnt = 0
Sample Run:
Let's roll some dice!
You rolled a 1
You rolled a 4
You rolled a 6
You rolled a 5
You rolled a 6
You rolled a 3
You rolled a 5
You rolled a 1
Three in a row in 8 rolls
Each time when you run the program, you will get different results.
Comments
Leave a comment