This repository was archived by the owner on Apr 24, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Expand file tree Collapse file tree 1 file changed +15
-3
lines changed Original file line number Diff line number Diff line change @@ -9,14 +9,17 @@ def toss_coin():
9
9
10
10
def main ():
11
11
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
12
14
# Clears the shell/terminal (where all the text is)
13
15
os .system ("cls" )
14
16
15
17
answer = input ("Pick a side for the coin toss (heads/tails): " )
16
18
17
19
# Input validation
18
20
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
20
23
continue
21
24
22
25
result = toss_coin ()
@@ -29,8 +32,17 @@ def main():
29
32
print ("OOF. Better luck next time." )
30
33
31
34
# 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
34
46
break
35
47
36
48
You can’t perform that action at this time.
0 commit comments