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

Commit 354d8b1

Browse files
Merge pull request #635 from Gabriela20103967/issue-634
enhanced the code of the coin Flip project.
2 parents 3207539 + f3e8778 commit 354d8b1

1 file changed

Lines changed: 58 additions & 22 deletions

File tree

projects/Coin Flip/coinflip.py

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,71 @@
22
import os
33

44

5-
while True:
6-
global answer_y
5+
def toss_coin():
6+
list1 = ["heads", "tails"]
7+
return random.choice(list1)
78

8-
# Clears the shell/terminal (where all the text is)
9-
os.system("clear")
109

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")
1214

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
1421

15-
# Picks randonly from the list
16-
random_s = random.choice(list1)
22+
result = toss_coin()
1723

18-
print("You got... " + random_s)
24+
print(f"You got... {result}")
1925

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.")
2330

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":
2734
break
2835

29-
if answer.lower() != random_s:
30-
# Tells the user if they lost
31-
print("OOF.")
3236

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

Comments
 (0)