Answer to Question #200237 in Python for Alex

Question #200237

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.

 


1
Expert's answer
2021-05-28T18:45:15-0400
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.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS