0

I'm making a program that flips heads or tails with an opponent. Here's the code:

import random
Choice=input("Do you want to flip a coin?")
while Choice=="yes":
   flip=input("Choose heads or tails!") 
   fliprandom=("heads","tails")
   opponentflip=random.choice(fliprandom)
   print ("you flipped",flip,"and your opponent flipped",opponentflip,":)")

My problem is that I want the user to be asked if they want to flip again, and exit the loop if the answer is 'no.' Right now, the user can say yes or no at the beginning, but the program keeps going if they say yes and doesn't give them any choice to continue or discontinue afterwards. How do I let the user choose to exit the loop?

matthew
  • 1
  • 2

1 Answers1

0

I saw two methods

First:

again = input("Do you want to flip a coin?").strip().lower()

while again == "yes":
    # flip a coin
    again = input("Do you want to flip a coin?").strip().lower()

Second:

while True
    again = input("Do you want to flip a coin?").strip().lower()
    if again != "yes":
        break
    # flip a coin

In first method you can add "again" in second text - "Do you want to flip a coin again?"

furas
  • 134,197
  • 12
  • 106
  • 148