Skip to content

Commit

Permalink
Merge pull request #703 from Xia3412/coinflip-branch
Browse files Browse the repository at this point in the history
Made changes to the coinflip.py file
  • Loading branch information
Mrinank-Bhowmick authored Feb 10, 2024
2 parents f9a96bd + ae04d4d commit cf2889c
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions projects/Coin Flip/coinflip.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ def toss_coin():

def main():
while True:
flag = False # Declaring a variable which will be used afterwards to break out of 2 nested while loops
# at the same time
# Clears the shell/terminal (where all the text is)
os.system("cls")

answer = input("Pick a side for the coin toss (heads/tails): ")

# Input validation
if answer.lower() not in ["heads", "tails"]:
print("Invalid input. Please enter 'heads' or 'tails'.")
# removed the print statement here because its not required since the while loop clears
# the statement anyways
continue

result = toss_coin()
Expand All @@ -29,8 +32,17 @@ def main():
print("OOF. Better luck next time.")

# Ask the user if they want to play again
answer_y = input("Wanna play again? (yes/no): ")
if answer_y.lower() != "yes":
while True:
answer_y = input("Wanna play again? (yes/no): ")
if answer_y.lower() == "no":
flag = True
break
elif answer_y.lower() == "yes":
break # if answer_y is "yes" then break out of only the innermost while loop and start the game again
else:
continue

if flag: # Checking if the flag variable is True
break


Expand Down

0 comments on commit cf2889c

Please sign in to comment.