Lab 4, Part 2: More Conditionals Practice

There are many tasks in this part of the lab, each designed to practice conditionals, functions and the concept of divide, solve, and combine.

Task 1: Heads or tails

A U.S. quarter-dollar coin, showing both the heads and tails sides.

In the /lab04/conditionals.py file you are given the following two functions:

  1. flip: simulates flipping a coin and returns a string that is either heads or tails
  2. getUsersGuess: asks for and returns the user's guess as a String

Invoke these functions from the Python console to make sure you understand how they work before proceeding.

Is flip a fruitful or None function?

Is getUsersGuess a fruitful or None function?

Task 1A. coinGuess

Write a function called coinGuess that glues together the provided functions to accomplish the following:

  1. Prompts the user to enter a guess, heads or tails.
  2. Compares their guess against a value, head or tails, that your function “flips”.
  3. Prints the results of whether the user won the toss or not.

Here are some sample invocations of this function when invoked from the Python console:

>>> coinGuess()
  Guess heads or tails => tails
  Sorry, the coin showed heads.

>>> coinGuess()
  Guess heads or tails => heads
  Sorry, the coin showed tails.

>>> coinGuess()
  Guess heads or tails => tails
  Wahoo, you are right- tails it is!

Task 2: Let's go to the movies

A pair of movie tickets with popcorn behind them.

In this task we want to build a program that does the following:

  1. Asks the user for their age (you can utilize the provided getUsersAge function)
  2. Uses the given age value to determine a) how much a movie ticket costs, and b) what category their ticket falls into (e.g. adult, senior, child)
  3. Prints the results

In the spirit of divide, solve, and combine, we will be dividing this program into functions, and then we'll glue the pieces together for the end result. Each of the following parts guides you through one part of the whole program.

Part A: Calculate price of a ticket based on the age of the customer

Write a function called getTicketPrice that accepts an Integer, age, as a parameter and returns the cost of a movie ticket.

Note: This function should not ask the user for their age; it's only job is to take a age value and return the appropriate Integer. Thus, this function should not involve any input or print statements.

Here are some sample invocations of this function when invoked from Thonny's shell:

>>> getTicketPrice(15)
12

>>> getTicketPrice(9)
10

>>> getTicketPrice(55)
12

Part B: Determine the ticket category given the age

Write another function called getTicketCategory that also accepts an Integer, age, as a parameter and returns the String category of the movie ticket.

Here are the categories:

(Once again, no input or print statements are needed in this function.)

Here are some sample invocations of this function when invoked from Thonny's shell:

>>> getTicketCategory(15)
'adult'

>>> getTicketCategory(9)
'child'

>>> getTicketCategory(55)
'adult'

>>> getTicketCategory(99)
'senior'

Part C: Glue all the pieces together

You should now have 3 independent functions:

It's time to “glue” these functions together for our main interaction. Write a function called visitMovieCounter that does the following:

  1. Welcomes the user with a message that says Welcome to the movies!
  2. Prompts the user for their age.
  3. Prints a statement letting the user know how much their ticket costs, and what kind of ticket they're getting.

Here are some sample invocations of this function when invoked from the Python console:

>>> visitMovieCounter()
  Welcome to the movies!

  How old are you? 15
  That will be $12 for one adult ticket, please.

>>> visitMovieCounter()
   Welcome to the movies!

   How old are you? 9
   That will be $10 for one child ticket, please.

>>> visitMovieCounter()
   Welcome to the movies!

   How old are you? 55
   That will be $12 for one adult ticket, please.

>>> visitMovieCounter()
   Welcome to the movies!

   How old are you? 70
   That will be $8 for one senior ticket, please.

Side note: Both the getTicketPrice and getTicketCategory accept the age as a parameter and return a value based on that age. Some of you might be wondering if we could combine this into a single function that returns two values (age and category). The answer is yes, and we will be learning this soon in CS111!

Task 3: Automatic Quiz:

a plant, some fire, and some water each in a glass

Do you ever find yourself wondering “What element am I? Am I Earth? Water? Fire?” Now you can write a Python program to answer this compelling question.

In this task, you will create a simple quiz that asks the user three multiple choice questions, and then determines what “element” they are based on their responses.

Here is a sample quiz:

In[]: runQuiz()

Which food do you like the most?
1: artichokes
2: lettuce
3: kimchi
Select 1, 2 or 3: 3

Which weather do you like the most?
1: no humidity and sunny
2: light rainfall and rainbows
3: lightning storms
Select 1, 2 or 3: 3

Which candy do you like the most?
1: gummy worms
2: junior mints
3: atomic fire balls
Select 1, 2 or 3: 3

*************************
     You are fire
*************************

You have some DSC experience under your belt now, so you might feel inspired to tackle this problem on your own (and do your own dividing). If so, go for it! On the other hand, if you would like more structure, click here for some guidance in dividing the problem into smaller sections.

When you're done, test your quiz to make sure it works as expected.

Table of Contents