Answer to Question #180865 in Python for Angelie Suarez

Question #180865

create a program that should ask a user input either 0 (tails) or

1 (heads). Then the program will draw a coin flip. The program will declare the

user a winner if the result of the coin flip is equal to the user input. If the

user enters a value outside the given choices, it will display invalid choice.


1
Expert's answer
2021-04-13T03:30:31-0400
import random

try:
    choice = int(input("Enter your choice 0 (tail) or 1 (head): "))
except:
    print('Invalide input')
    exit()

if choice != 0 and choice != 1:
    print('Invalide choice!')
    exit()

coin = random.randint(0,1)
if choice == coin:
    print('You win!')
else:
    print('You lose.')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS