import random def Flip(): total = 0 while True: f = random.randint(0,1) side = "Tails" if (f == 0): side = "Heads" total += 1 print(side) command = input("Type 'Stop' if you want to leave the function.") if (command == "Stop"): break print("Leaving function.") return total x = Flip() print("Number of heads was " + str(x))