So you want the random randint to execute only 3 times. This is how
from random import randint
timesexecuted = int(0)
while timesexecuted < 3:
print(randint(0,12))
timesexecuted = timesexecuted + 1
#after this anything you want happen next.
This method is most efficient when you are looping things larger amounts of times, such as, you want to executed print(randint(0,12))
over hundreds of times, this method is probably easiest to write.
Explanation
timesexecuted
is set to int(0)
since no loops have been run and we need to define timesexecuted
. We need to run a loop 3 times so to do that, in the loop, we add on 1 to timesexecuted
with timesexecuted + 1
and so once timesexecuted
is up to 3 (starts on 0, does 1 loop, is now 1, after another loop, becomes 2, does a third loop, is now 3). And since we have the loop under while timesexecuted < 3:
, as timesexecuted
is now 3 because the loop has run 3 times, timesexecuted < 3
is no longer true and the loop will run no more. The 3
can also be changed to any number, making this method probably the best if looping many times. (eg. Looping this program 200 times with while timesexecuted < 200:
)
Hope I helped.