dimanche 31 mai 2020

Python choices; Having a variable value for number of times the choice can be carried out

I'm attempting to keep getting random choices until the condition in the if block is met(i.e when three consecutive choices return the same result). The challenge is that I don't know how to set the choice to continue until the condition is met because it seems k must be specified. Thus, I cannot have a variable value for k which is determined by when the condition is met.


#import random module
import random

# initiate x which would contain predictions
x = 0

#define prediction; which would continue until 3 consecutive predictions get same outcome
while True:
   x = (random.choices(['H','T'], k = 10 ))
   print(x)
   if x[-3:] == ['H','H','H'] or x[-3:] == ['T','T','T']:
      break




Aucun commentaire:

Enregistrer un commentaire