أحتاج بعض النصائح لتصغير الكود و تحسينة
import random
print("""Welcome to the Coin Guessing Game!
Choose a method to toss the coin:
1. Using random.randint() (to simulate a coin toss)
2. Using random.random() (to simulate a coin toss)""")
choice = int(input("Enter your choice (1 or 2):"))
if choice == 1:
Guess = input("Enter your Guess (Heads or Tails):").lower()
random_my = random.randint(1, 2)
if Guess == "heads":
Guess = 1
win = "heads"
not_win = "tails"
elif Guess == "tails":
Guess = 2
win = "tails"
not_win = "heads"
else:
print("Invalid input. Please enter either 'heads' or 'tails'.")
exit()
if Guess == random_my:
print("Congratulations! You won!")
print(f"The computer's coin toss result was: {win}")
else:
print("Sorry, you lost!")
print(f"The computer's coin toss result was: {not_win}")
elif choice == 2:
Guess = input("Enter your Guess (Heads or Tails):").lower()
random_my = random.random()
if Guess == "heads":
Guess = 0
win = "heads"
not_win = "tails"
elif Guess == "tails":
Guess = 1
win = "tails"
not_win = "heads"
else:
print("Invalid input. Please enter either 'heads' or 'tails'.")
exit()
if random_my < 0.5:
random_my = 0
else:
random_my = 1
if Guess == random_my:
print("Congratulations! You won!")
print(f"The computer's coin toss result was: {win}")
else:
print("Sorry, you lost!")
print(f"The computer's coin toss result was: {not_win}")
else:
print("Invalid choice. Please select either 1 or 2.")