Assignment 3 – Vending Machine

Learning Objective

Write a Python program that uses arithmetic operators and branching statements.

Assignment Description

Write a program that calculates the amount of change to be returned from a vending machine using Harry Potter currency. In the wizarding world of Harry Potter, the currency system is not based off dollars and cents, but instead 3 different coins: knuts, sickles, and galleons. The conversion values for these coins in knuts are as follows:

  • 1 knut = 1 knut
  • 1 sickle = 29 knuts
  • 1 galleon = 493 knuts

The knut is similar to a cent (or penny) in U.S. currency, and the sickle is similar to a U.S. quarter. One dollar equals 4 quarters or 100 cents. In Harry Potter currency, one galleon equals 17 sickles or 493 knuts.

The vending machine dispenses 4 options: Assortment of candy for 11 sickles and 7 knuts, Butterbeer for 2 sickles, Daily Prophet for 5 knuts, and Quill for 6 sickles.

The vending machine accepts galleons, sickles, and knuts for payment.

Steps

  1. In PyCharm (Community Edition), open your existing ITP115 project.
  2. Under the Assignments directory, create a new directory called a3_last_first where last is your last/family name and first is your preferred first name. Use all lowercase letters.
  3. In the directory, create a new Python file called assignment3.py.
  4. At the top of the file, put comments in the following format and replace the name, email, and section with your actual information:
  1. # ITP 115, Fall 2022

This content is protected and may not be shared, uploaded, or distributed.

ITP 115

2022-08-21

# Section: number or nickname
# Assignment 3
# Description:
# Describe what this program does such as:
# This program creates a Harry Potter vending machine.
# User enters payment and the program determines the change.

  1. Display the following to the user:
  1. Please select an item from the vending machine:
  2. a) Assortment of Candy for 11 sickles and 7 knuts
  3. b) Butterbeer for 2 sickles
  4. c) Quill for 6 sickles
  5. d) Daily Prophet for 5 knuts
  1. Get input from the user for their menu choice using the following prompt:

Choice: b

  1. Your code must handle the user entering upper case and lower case letters. For example, the user can enter b or B to select a Butterbeer. Create some variables to hold the item (string) and the cost (int). The user input is shown in green.
  2. Your code must handle the user entering an invalid menu choice. If the user enters something other than the choices on the menu, then tell them they have entered an invalid option. Since we have not covered how to repeat code yet, just pick an option for them. Here is an example:
  1. You entered an invalid choice, thus the item selected is the Quill
  1. Have the user enter in the number of galleon, sickles and knuts. When testing, the grader will only enter in an integer value.
  1. Please pay by entering the number of each coin
  2. Number of galleons: 1
  3. Number of sickles: 0
  4. Number of knuts: 0

10.Calculate the cost of the item in knuts and put that into a variable. Calculate the payment in knuts and put that into a variable. Print out the cost and the payment. Here is an example:

   Cost in knuts: 326

   Payment in knuts: 493

Page 2 of 6

ITP 115 2022-08-21

11.If the payment is less than the cost, then print a message to the user saying that the user will not receive their item. Here is an example:

   You did not enter enough money and will not receive the Quill

12.If the payment is enough, then calculate the change in knuts. Print the item and the change in knuts. Using the division and modulo operators, calculate the change that will be dispensed in galleons, sickles, and knuts. Print the number of each coin for the change. Feel free to create variables to hold information.

o You must dispense change in the largest possible coins. For example, if an item costs 326 knuts and the payment is one galleon (493 knuts), the change is 167 knuts. You may not simply return 167 knuts; it should be 5 sickles, and 22 knuts.

o Example of output:

   You purchased the Assortment of Candy

   Your change is 167 knuts

   You will be given

     Galleons: 0

     Sickles: 5

     Knuts: 22

13.Be sure to comment your code. This means that there should be comments throughout your code. Generally, a comment for every section of code explaining what it does. Points will be deducted for not having comments.

14.Follow coding conventions.

15.Test the program. Look at the Sample Output below. Assignments that do not run are subject to 20% penalty.

16.Prepare your submission:
o Find the a3_last_first folder on your computer and compress it. This cannot be

done within PyCharm.

o On Windows, use File Explorer to select the folder. Right click and select the Send to -> Compressed (zipped) folder option. This will create a zip file.

o On Mac OS, use Finder to select the folder. Right click and select the Compress “FolderName” option. This will create a zip file.

17.Upload the zip file to your Blackboard section:

Page 3 of 6

ITP 115

2022-08-21

o On Blackboard, navigate to the appropriate item.
o Click on the specific item for this assignment.
o Click on the Browse Local Files button and select the file. o Click the Submit button.

Grading

  • §  This assignment is worth 30 points.
  • §  Make sure that you the program runs. Points will be taken off if the graders have to

edit the source code to test your program.

  • §  Make sure to submit your assignment correctly as described above. Points will be taken off for improper submission.

Item Points

Menu 4

User Input for item and payment 10

Error checking for uppercase and invalid option 5

Math operations 5

Output to user 5

Comments, followed instructions, and proper submission 1

Total

30

Page 4 of 6

ITP 115

2022-08-21

Sample Output

Example 1:

Please select an item from the vending machine:

  1. a) Assortment of Candy for 11 sickles and 7 knuts
  2. b) Butterbeer for 2 sickles
  3. c) Quill for 6 sickles
  4. d) Daily Prophet for 5 knuts

Choice: A

Please pay by entering the number of each coin

Number of galleons: 1

Number of sickles: 0

Number of knuts: 0

Cost in knuts: 326

Payment in knuts: 493

You purchased the Assortment of Candy

Your change is 167 knuts

You will be given

     Galleons: 0

     Sickles: 5

     Knuts: 22

Example 2:

Please select an item from the vending machine:

  1. a) Assortment of Candy for 11 sickles and 7 knuts
  2. b) Butterbeer for 2 sickles
  3. c) Quill for 6 sickles
  4. d) Daily Prophet for 5 knuts

Choice: c

Please pay by entering the number of each coin

Number of galleons: 0

Number of sickles: 2

Number of knuts: 42

Cost in knuts: 174

Payment in knuts: 100

You did not enter enough money and will not receive the Quill

Page 5 of 6

ITP 115

2022-08-21

Example 3:

Please select an item from the vending machine:

  1. a) Assortment of Candy for 11 sickles and 7 knuts
  2. b) Butterbeer for 2 sickles
  3. c) Quill for 6 sickles
  4. d) Daily Prophet for 5 knuts

Choice: e

You entered an invalid choice, thus the item selected is the Quill

Please pay by entering the number of each coin

Number of galleons: 0

Number of sickles: 7

Number of knuts: 3

Cost in knuts: 174

Payment in knuts: 206

You purchased the Quill

Your change is 32 knuts

You will be given

     Galleons: 0

     Sickles: 1

     Knuts: 3

Page 6 of 6