CS21 Lab 3: if statements and randomness

Due Saturday, September 30, before midnight


Make sure all programs are saved to your cs21/labs/03 directory!


The goals for this lab assignment are:


1. Ye Olde Pizza Shoppe

The local pizza shop has restructured their menu pricing for students. Going forward, one large pizza will cost $9, two large pizzas will cost $15, three large pizzas will cost $20, and any additional pizzas after that will cost just $4 each. No matter how many pizzas you order, each topping will cost $1 per pizza.

Here are some examples:

Write a program called pizza.py that asks the customer how many pizzas they want to order. Then, for each pizza, ask them how many toppings they want on their pizza. After they have entered their order, display the total price.

You can assume the customer types in positive integers at each prompt and that they order at least one pizza.

Here are some sample runs of the program. In this example, the customer ordered one pizza with two toppings:

$ python3 pizza.py
Welcome to Phoenix Pizza!
How many pizzas do you want to order: 1

Pizza # 1
Number of toppings on this pizza: 2

You ordered 1 pizzas with 2 total toppings.
Your cost is $ 11
Thanks for your order!

In this example, the customer ordered 3 pizzas: one with no toppings, one with two toppings and one with one topping:

$ python3 pizza.py
Welcome to Phoenix Pizza!
How many pizzas do you want to order: 3

Pizza # 1
Number of toppings on this pizza: 0

Pizza # 2
Number of toppings on this pizza: 2

Pizza # 3
Number of toppings on this pizza: 1

You ordered 3 pizzas with 3 total toppings.
Your cost is $ 23
Thanks for your order!

In this example, the customer ordered 6 pizzas and none of them had toppings:

$ python3 pizza.py
Welcome to Phoenix Pizza!
How many pizzas do you want to order: 6

Pizza # 1
Number of toppings on this pizza: 0

Pizza # 2
Number of toppings on this pizza: 0

Pizza # 3
Number of toppings on this pizza: 0

Pizza # 4
Number of toppings on this pizza: 0

Pizza # 5
Number of toppings on this pizza: 0

Pizza # 6
Number of toppings on this pizza: 0

You ordered 6 pizzas with 0 total toppings.
Your cost is $ 32

Thanks for your order!

2. Multiplication Flash Cards

Write a program, called flashcards.py, that generates multiplication flash cards for children who want practice multiplying small numbers.

Your program will first ask the child what their level is and then you will ask them how many questions they'd like. Your program will generate multiplication questions of the appropriate level and then tell the child how well they did at the end.

For each question you will randomly choose two numbers appropriate to the child's level. Display an appropriate message after each answer provided.

If they got more than 75% correct, print "Well done!" otherwise print "Keep practicing!"

You can assume the child only provides valid inputs (e.g. don't worry if they type something that isn't an integer).

Here is a sample run of the program:

$ python3 flashcards.py
Multiplication Flash Cards

Enter your math level: 2
How many flash cards do you want? 5

Question 1 :  10 * 9
Answer: 90
Correct!

Question 2 :  12 * 8
Answer: 86
Sorry, the answer was 96

Question 3 :  5 * 9
Answer: 59
Sorry, the answer was 45

Question 4 :  5 * 11
Answer: 55
Correct!

Question 5 :  1 * 2
Answer: 2
Correct!

You got 3 right out of 5
Keep practicing!

3. Gift Card Generator

At some point you may have received a gift card to an online store like the iTunes store or amazon.com. These gift cards contain a code that you you can type into your account. These codes look like random strings of letters, numbers and dashes. For example, an amazon.com gift card looks like this: PYXC-9C9CT2-KBAB.

For this question, write a program called giftcard.py that generates random gift card codes from a template supplied by the user. The user will enter a template that contains the characters A, # and -. You will use that template to generate a random gift card that matches the template. Each of the A characters in the template will be used to randomly generate a capital letter (A-Z). Each of the # characters in the template will be used to randomly generate a numeric digit (0-9). Each of the - characters will remain as dashes. (If the user enters any other character in the template, just ignore it.)

To generate a random letter, you can use random.choice to select from the sequence of letters A-Z. This also works for generating a random numeric digit:

import random

random_letter = random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
random_digit = random.choice('0123456789')

Here are runs of my program. Your output will be different because of the randomness of the letters and numbers

$ python3 giftcard.py
Enter the gift card template: AAAA-#A#AA#-AAAA
Your gift card code is: ITRZ-3G1RC9-SGSK

Here's another run with the same template:

$ python3 giftcard.py
Enter the gift card template: AAAA-#A#AA#-AAAA
Your gift card code is: AELL-6G0ZT9-LXYC

And another with a run with an all-numeric template:

$ python3 giftcard.py
Enter the gift card template: ########
Your gift card code is: 77103945

Here's a run of the program where the user entered an invalid template because it contained the letters B and C (which are not valid because only A, # and - are valid). Our code generator will just ignore those invalid characters and generate a code that uses the remaining valid parts of the template:

$ python3 giftcard.py
Enter the gift card template: A#B-CAC-AAA
Your gift card code is: W9-Q-TGM

4. Answer the Questionnaire

Please edit the QUESTIONS-03.txt file in your cs21/labs/03 directory and answer the questions in that file.


Turning in your labs....

Don't forget to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.