""" Authors: Peter Mawhorter and Sohie Lee Consulted: Date: 2022-9-26 Purpose: CS111 Lab 04 SOLUTIONS Practice using conditionals and functions to build an interactive program that asks the user information, and then calculates ticket prices according to age. Filename: conditionals.py """ import random #-----------------------# # Task 1 Heads or tails # #-----------------------# def flip(): ''' Provided function (DON'T modify); simulates a coin flip returning either heads or tails as a String ''' coin = random.randint(1, 2) if coin == 1: return 'heads' else: return 'tails' def getUsersGuess(): ''' Provided function (DON'T modify); Asks for user to guess heads or tails and returns the String ''' return input('Guess heads or tails => ') # Write the rest of your code for Task 1 here. def coinGuess(): ''' Play the coin flip game once ''' # store the coin flip in a variable. Only want to flip the coin once. headsOrTails = flip() usersGuess = getUsersGuess() # store the user's guess in a variable if usersGuess == headsOrTails: print('Wahooo you are right- ' + headsOrTails + ' it is!') else: print('Sorry, the coin showed ' + headsOrTails + '.') #-------------------------------# # Task 2 Let's go to the movies # #-------------------------------# def getUsersAge(): ''' Provided function (DON'T modify); Prompts user to enter age and returns the value as an integer. Will be used in this task and also Task 3. ''' return int(input('How old are you? ')) # Write your code for Task 2 here. def getTicketPrice(age): ''' Returns the movie ticket price given the age ''' if age < 12: return 10 elif age > 55: return 8 else: return 12 def getTicketCategory(age): ''' Returns the movie ticket category given the age ''' if age < 12: return 'child' elif age > 55: return 'senior' else: return 'adult' def visitMovieCounter(): ''' Simulates a movie ticket-purchasing experience ''' print('Welcome to the movies! ') # Prompt the user for their age age = getUsersAge() # Figure out the ticket price and ticket category using our helper # functions ticketPrice = getTicketPrice(age) ticketCategory = getTicketCategory(age) # Print the results print( 'That will be $' + str(ticketPrice) + ' for one ' + ticketCategory + ' ticket, please.' ) #----------------------------------------------# # Task 3 Automatic quiz: What element are you? # #----------------------------------------------# def askUserForAnswer(): ''' Provided function; Ask user to choose answer of 1, 2 or 3 in multiple choice question and returns selected integer ''' return int(input("Select 1, 2 or 3: ")) # Write your code for Task 3 here. def firstQuestion(): ''' Asks the first question Answers should fall into earth/water/fire categories in that order ''' print('Which food do you like the most?') print('1: artichokes') print('2: lettuce') print('3: kimchi') def secondQuestion(): ''' Asks second question Answers should fall into earth/water/fire categories in that order ''' print('Which weather do you like the most?') print('1: no humidity and sunny') print('2: light rainfall and rainbows') print('3: lightning storms') def thirdQuestion(): ''' Asks third question Answers should fall into earth/water/fire categories in that order''' print('Which candy do you like the most?') print('1: gummy worms') print('2: junior mints') print('3: atomic fire balls') def determineElement(a, b, c): ''' Returns an element based on the multiple choice answers ''' sum = a + b + c # Selected all 3s in quiz if sum > 8: return 'fire' # Selected combination of 1s and 2s, leaning towards the side of 2 elif sum > 4: return 'water' # Selected combination of 1s and 2s, leaning towards side of 1 else: return 'earth' def runQuiz(): ''' Plays a highly intellectual quiz game with life-changing results ''' firstQuestion() q1Answer = askUserForAnswer() secondQuestion() q2Answer = askUserForAnswer() thirdQuestion() q3Answer = askUserForAnswer() yourElement = determineElement(q1Answer, q2Answer, q3Answer) print('*************************') print(' You are ' + str(yourElement)) print('*************************') #--------------# # Testing code # #--------------# # Add function calls here to test the functions you're working on above print("Start of testing.") # call your function(s) here # coinGuess() # print(getTicketCategory(5)) # should be child # print(getTicketCategory(65)) # should be senior # print(getTicketCategory(12)) # should be adult # visitMovieCounter() #firstQuestion() #secondQuestion() #thirdQuestion() #print(determineElement(3,3,3)) # fire #print(determineElement(1,2,3)) # water #print(determineElement(1,1,1)) # earth #print(determineElement(1,2,2)) # earth #runQuiz() print("Done with testing.")