This content originally appeared on DEV Community and was authored by عاشق الابداع
import random
print("Welcome to the Coin Guessing Game!")
print ("Choose a method to toss the coin: \n 1. Using random.random() \n 2. Using random.randint()")
choice = input("Enter your choice (1 or 2): \n ")
guess = input("Enter your Guess (Heads or Tails): \n ").lower()
if guess not in ["tails", "heads"]:
print("Invalid choice. Please select either Heads or Tails")
else:
if choice == "1":
res = "heads" if random.randint(0,1) ==1 else "tails"
if res == guess:
print("Congratulations! You won!")
print(f"The computer's coin toss result was: {res}")
elif res != guess:
print("Sorry, You lost!")
print(f"The computer's coin toss result was: {res}")
elif choice == "2":
res = "heads" if random.random() >=0.5 else "tails"
if res == guess:
print("Congratulations! You won!")
print(f"The computer's coin toss result was: {res}")
elif res != guess:
print("Sorry, You lost!")
print(f"The computer's coin toss result was: {res}")
else:
print("Invalid choice. Please select either 1 or 2")
This content originally appeared on DEV Community and was authored by عاشق الابداع
عاشق الابداع | Sciencx (2024-10-27T20:03:22+00:00) Guess Heads or Tails between you and the computer. Retrieved from https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.