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...
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)
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...
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.
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.
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" } ...
python: ask the user to input a sequence of positive numbers. the end of the sequence...
python: ask the user to input a sequence of positive numbers. the end of the sequence is determined when the user enters a negative number. print out the maximum number from the sequence. output: keep entering positive numbers. to quit, input a negative number. enter a number: 67 enter a number: 5 enter a number: 8 enter a number: -3 largest number entered: 67 (note: i do not want to ask the user how many numbers they will input)
In your python program, ask the user to enter the annual income of an employee and...
In your python program, ask the user to enter the annual income of an employee and the years of experience. Pass these data to a function. The function decides if an employee does qualify for a loan or does not, then it prints a message based on the following rules: If the income is equal or greater than $40000, the function checks if the employee’s years of experience is greater than 4, if so the employee gets $6000 loan; otherwise,...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT