Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit cf2889c

Browse files
Merge pull request #703 from Xia3412/coinflip-branch
Made changes to the coinflip.py file
2 parents f9a96bd + ae04d4d commit cf2889c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

projects/Coin Flip/coinflip.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ def toss_coin():
99

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

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

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

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

3134
# Ask the user if they want to play again
32-
answer_y = input("Wanna play again? (yes/no): ")
33-
if answer_y.lower() != "yes":
35+
while True:
36+
answer_y = input("Wanna play again? (yes/no): ")
37+
if answer_y.lower() == "no":
38+
flag = True
39+
break
40+
elif answer_y.lower() == "yes":
41+
break # if answer_y is "yes" then break out of only the innermost while loop and start the game again
42+
else:
43+
continue
44+
45+
if flag: # Checking if the flag variable is True
3446
break
3547

3648

0 commit comments

Comments
 (0)