In: Computer Science
In Python
Randomly choose either heads or tails ('h' or 't'). Ask the user to make their choice. If their choice is the same as the randomly selected value, display the message 'You win', otherwise display 'Bad luck'. At the end, tell the user if the computer selected heads or tails.
Python code:
import random
#Computer choice out of 'h' or 't'
computer_choice = random.choice(['h','t'])
print("Please enter your choice:\nEnter 'h' for heads or 't' for
tails")
#Choice of user
user_choice = input()
#Check if user choice matches computer choice
if computer_choice == user_choice:
print("You win")
else:
print("Bad Luck")
#Print choice of computer
print("Computer had selected ",end="")
if computer_choice == 'h':
print("heads")
else:
print("tails")
Please refer screenshot of the code for indentation.
Output:
Test case:1
Test case 2: