lundi 5 juin 2023

Using random.choice in a loop

I'm new to learning Python, currently learning Modules. I'm stuck on this:

  1. Randomly choose an element until you choose "yellow", incrementing flips each time
  2. Print a message each time including how many flips have been executed and what the result was
import random 

flips = 0
sides = ["blue", "blue", "blue", "blue", "yellow", "blue"]

side = random.choice(sides) 

for side in sides:
    if side == "blue":
        print("you flipped a blue")
        flips += 1
    elif side =="yellow":
        print("you flipped a yellow!")
        flips += 1
        break

the output i got for this was:

you flipped a blue
you flipped a blue
you flipped a blue
you flipped a blue
you flipped a yellow!
5

I can see here that it's just iterating through my sides list till it gets to "yellow". How do I fix this to actually be random??




Aucun commentaire:

Enregistrer un commentaire