Question

In: Computer Science

In Python Randomly choose either heads or tails ('h' or 't'). Ask the user to make...

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.

Solutions

Expert Solution

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:


Related Solutions

The list of sequences of Heads (H) and Tails (T) in four coin flips with various...
The list of sequences of Heads (H) and Tails (T) in four coin flips with various numbers of heads is as follows: 0 Heads TTTT 1 Head HTTT THTT TTHT TTTH 2 Heads HHTT HTHT THHT HTTH THTH TTHH 3 Heads HHHT HHTH HTHH THHH 4 Heads HHHH Write out the list of sequences of Heads (H) and Tails (T) in five coin flips with 0 Heads, 1 Head, 2 Heads, 3 Heads, 4 Heads and 5 Heads. Describe how...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number...
Flowchart + Python. Ask the user for a value. Then, ask the user for the number of expressions of that value. Use While Loops to make a program. So then, it should be so that 5 expressions for the value 9 would be: 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 Flowcharts and Python Code. Not just Python code. I cannot read handwriting....
In python. Projectile motion: Write a python program that will ask the user for      an...
In python. Projectile motion: Write a python program that will ask the user for      an initial height y0, initial velocity v, launch angle theta, and mass m.      Create two functions, one that will calculate max height      of the projectile, and one that will calculate the range. Ask the     user which one he/she would like to calculate, then present them with the answer. (use kg, m and m/s)
A coin is flipped repeatedly until either two heads appear in a row or two tails...
A coin is flipped repeatedly until either two heads appear in a row or two tails appear in a row(and then stop). Find the exact answer for P(two heads in a row appears before two tails in a row) for a coin with probability p of getting heads.
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the...
PYTHON Ask the user to enter the number of students in the class. Thereafter ask the user to enter the student names and scores as shown. Output the name and score of the student with the 2nd lowest score. NOTE: You may assume all students have distinct scores between 0 and 100 inclusive and there are at least 2 students NOTE: You may only use what has been taught in class so far for this assignment. You are not allowed...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Calculating Delivery Cost Program in Python write a program in Python that will ask a user...
Calculating Delivery Cost Program in Python write a program in Python that will ask a user to enter the purchase total, the number of the items that need to be delivered and delivery day. Then the system displays the cost of delivery along with the total cost. Purchase total > $150 Yes Number of the items (N) N<=5 N>=6 Delivery day Same Day Next Day Same Day Next Day Delivery charges ($) 8 N * 1.50 N * 2.50 N...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer...
[C#] Randomly generate two numbers that user chooses and then ask the user what the answer is when some operator is applied. If user is correct, they will be congratulated. If they are wrong, they will be given the correct answer. This should be repeated based on how many times is chosen by user. I have the code which will do the following below: User wants to know the answer to x % y, What is smallest value of x:...
PYTHON Modify the program in section Ask the user for a first name and a last...
PYTHON Modify the program in section Ask the user for a first name and a last name of several people.  Use a loop to ask for user input of each person’s first and last names  Each time through the loop, use a dictionary to store the first and last names of that person  Add that dictionary to a list to create a master list of the names  Example dictionary: aDict = { "fname":"Douglas", "name":"Lee" } ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT