Guess Heads or Tails between you and the computer

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 T…


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 عاشق الابداع


Print Share Comment Cite Upload Translate Updates
APA

عاشق الابداع | 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/

MLA
" » Guess Heads or Tails between you and the computer." عاشق الابداع | Sciencx - Sunday October 27, 2024, https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/
HARVARD
عاشق الابداع | Sciencx Sunday October 27, 2024 » Guess Heads or Tails between you and the computer., viewed ,<https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/>
VANCOUVER
عاشق الابداع | Sciencx - » Guess Heads or Tails between you and the computer. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/
CHICAGO
" » Guess Heads or Tails between you and the computer." عاشق الابداع | Sciencx - Accessed . https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/
IEEE
" » Guess Heads or Tails between you and the computer." عاشق الابداع | Sciencx [Online]. Available: https://www.scien.cx/2024/10/27/guess-heads-or-tails-between-you-and-the-computer/. [Accessed: ]
rf:citation
» Guess Heads or Tails between you and the computer | عاشق الابداع | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.