Home CPSC 110

Lab 3: Snack Bar

 

Objective

To gain experience writing programs which take input, perform calculations and output results.


 

Task

For this lab, you'll be creating a program that could be used for workers at a snack bar. The snack bar sells 4 items at different prices:

ItemCost
Bottles of water.50
Candy bars1.00
Bags of popcorn0.25
Cans of soda0.75

Your program will help the snack bar workers by figuring out the total cost of all items that a customer wants to purchase. It will ask how many items of each type the customer has selected, calculate the total, and print it to the screen.

For example, if someone purchases 2 bottles of water, 4 candy bars, 4 bags of popcorn and 2 cans of soda, the program should calculate their total cost as $7.50.


 

Details

  1. Start by asking the user how many bottles of water were purchased, and store the result in a variable.
  2. Do the same thing for the other three items. You will need to use different variable names for each of the four items!
  3. After getting all of the input, you will need to calculate the total cost. This can be done by multiplying each item's count by its cost, and then adding them all together. Save the result of this calculation into another variable.
  4. Finally, print the total cost to the screen so the snack bar worker knows the result.

 

Testing

Your program should work for any number of items that the user selects. Of course you can't test every possible input, but you should test with several different ones.

Below are a couple of example runs you can try out. The answers that the user gives are given in bold face:

How many bottles of water? 2
How many candy bars? 4
How many bags of popcorn? 4
How many cans of soda? 2
The total cost is 7.5
How many bottles of water? 1
How many candy bars? 2
How many bags of popcorn? 3
How many cans of soda? 4
The total cost is 6.25
How many bottles of water? 100
How many candy bars? 0
How many bags of popcorn? 0
How many cans of soda? 1
The total cost is 50.75

 

Submitting

When you are finished, please submit the .py file for the lab on Canvas. To do so, you'll need to navigate to where you saved the file on your computer.

Copyright © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.