Python doesn't have anything like a do-while loop. So this is a technique you can do in any language called "loop and a half". It's pretty cool. I use them a lot.
from __future__ import print_function
import random
input=raw_inputdef main():
streak =0whileTrue:
gotHeads = random.random() <0.5if gotHeads:
coin ="HEADS"else:
coin ="TAILS"print("You flip a coin and it is... "+ coin)
if gotHeads:
streak +=1print("\tThat's {} in a row...".format(streak))
again =input("\tWould you like to flip again (y/n)? ")
else:
print("\tYou lose everything!")
print("\tShould've quit while you were aHEAD....")
streak =0
again ="n"if again !="y":
breakprint("Final score: {}".format(streak))
if__name__=="__main__":
main()
What You Should See
You flip a coin and it is... HEADS
That's 1 in a row...
Would you like to flip again (y/n)? y
You flip a coin and it is... HEADS
That's 2 in a row...
Would you like to flip again (y/n)? n
Final score: 2
Basically you make an infinite loop on purpose. Then, at the bottom of the loop, you test whatever condition you want. If it's true, you break out of the loop.