|
2 | 2 | import os |
3 | 3 |
|
4 | 4 |
|
5 | | -while True: |
6 | | - global answer_y |
| 5 | +def toss_coin(): |
| 6 | + list1 = ["heads", "tails"] |
| 7 | + return random.choice(list1) |
7 | 8 |
|
8 | | - # Clears the shell/terminal (where all the text is) |
9 | | - os.system("clear") |
10 | 9 |
|
11 | | - answer = input("Pick a side for the coin toss: ") |
| 10 | +def main(): |
| 11 | + while True: |
| 12 | + # Clears the shell/terminal (where all the text is) |
| 13 | + os.system("cls") |
12 | 14 |
|
13 | | - list1 = ["heads", "tails"] |
| 15 | + answer = input("Pick a side for the coin toss (heads/tails): ") |
| 16 | + |
| 17 | + # Input validation |
| 18 | + if answer.lower() not in ["heads", "tails"]: |
| 19 | + print("Invalid input. Please enter 'heads' or 'tails'.") |
| 20 | + continue |
14 | 21 |
|
15 | | - # Picks randonly from the list |
16 | | - random_s = random.choice(list1) |
| 22 | + result = toss_coin() |
17 | 23 |
|
18 | | - print("You got... " + random_s) |
| 24 | + print(f"You got... {result}") |
19 | 25 |
|
20 | | - if answer.lower() == random_s: |
21 | | - # Tells the user if they won |
22 | | - print("Nice you won the coin toss!!") |
| 26 | + if answer.lower() == result: |
| 27 | + print("Nice, you won the coin toss!!") |
| 28 | + else: |
| 29 | + print("OOF. Better luck next time.") |
23 | 30 |
|
24 | | - # Asks the user if they want to play again |
25 | | - answer_y = input("Wanna play agian? ") |
26 | | - if answer_y.lower() == "no": |
| 31 | + # Ask the user if they want to play again |
| 32 | + answer_y = input("Wanna play again? (yes/no): ") |
| 33 | + if answer_y.lower() != "yes": |
27 | 34 | break |
28 | 35 |
|
29 | | - if answer.lower() != random_s: |
30 | | - # Tells the user if they lost |
31 | | - print("OOF.") |
32 | 36 |
|
33 | | - # Asks the user if they want to play again |
34 | | - answer_y = input("Wanna play agian? ") |
35 | | - if answer_y.lower() == "no": |
36 | | - break |
| 37 | +if __name__ == "__main__": |
| 38 | + main() |
| 39 | + |
| 40 | + |
| 41 | +# while True: |
| 42 | +# global answer_y |
| 43 | +# |
| 44 | +# # Clears the shell/terminal (where all the text is) |
| 45 | +# os.system("clear") |
| 46 | +# |
| 47 | +# answer = input("Pick a side for the coin toss: ") |
| 48 | +# |
| 49 | +# list1 = ["heads", "tails"] |
| 50 | +# |
| 51 | +# # Picks randonly from the list |
| 52 | +# random_s = random.choice(list1) |
| 53 | +# |
| 54 | +# print("You got... " + random_s) |
| 55 | +# |
| 56 | +# if answer.lower() == random_s: |
| 57 | +# # Tells the user if they won |
| 58 | +# print("Nice you won the coin toss!!") |
| 59 | +# |
| 60 | +# # Asks the user if they want to play again |
| 61 | +# answer_y = input("Wanna play agian? ") |
| 62 | +# if answer_y.lower() == "no": |
| 63 | +# break |
| 64 | +# |
| 65 | +# if answer.lower() != random_s: |
| 66 | +# # Tells the user if they lost |
| 67 | +# print("OOF.") |
| 68 | +# |
| 69 | +# # Asks the user if they want to play again |
| 70 | +# answer_y = input("Wanna play agian? ") |
| 71 | +# if answer_y.lower() == "no": |
| 72 | +# break |
0 commit comments