Lab 4, Part 2: Conditionals

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

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: What can you do?

A collage depicting a washing machine, a person driving, and a 'Vote' button.

In this task, we want a program that will ask the user for their age and report back to them about what “privileges” they have at that age.

Here are the privileges as we've defined them:

Note how these privileges cascade; for example, if you're 18 you can empty the diswasher, and drive, and vote.

To assist with this task, in /lab04/conditionals.py you are provided with a function called getUsersAge that prompts the user for their age, and returns the value as an Integer.

Invoke this function in Thonny's shell (the bottom half) to make sure you understand how it works.

>>> getUsersAge()

  How old are you? 32
32

>>> 

Now, your task is to write a function called whatCanYouDo that does the following:

  1. Prompts for the user for their age (using the provided getUsersAge function)
  2. Prints a list of the things the user can do at that age

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

>>> whatCanYouDo()

  How old are you? 25
  You can purchase alcohol
  You can vote
  You can drive
  You can empty the dishwasher

>>> whatCanYouDo()

  How old are you? 18
  You can vote
  You can drive
  You can empty the dishwasher

>>> whatCanYouDo()

  How old are you? 16
  You can drive
  You can empty the dishwasher

>>> whatCanYouDo()

  How old are you? 10
  You can empty the dishwasher

>>> whatCanYouDo()

  How old are you? 5
  You can empty the dishwasher

>>> whatCanYouDo()

  How old are you? 4
  No privileges yet

Task 3: 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 same provided getUsersAge function from Task 2 above)
  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, conquer, and glue, we will be dividing this program into functions, and then we'll glue the pieces together for the end result.

Task 3 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

Task 3 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'

Task 3 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!

Table of Contents