INDEX
S.NO. TOPIC PAGE NO.
1. INTRODUCTION
2. ABOUT THE PROJECT
3. WORKING DESCRIPTION
4. SOURCE CODE
5. INPUT/OUTPUT INTERFACE
6. CONCLUSION
1
INTRODUCTION
PYTHON DESCRIPTION:
Python is an interpreted, object-oriented, high-level
programming language with dynamic semantics. Its high-level built in data
structures, combined with dynamic typing and dynamic binding, make it very
attractive for Rapid Application Development, as well as for use as a scripting
or glue language to connect existing components together. Python's simple, easy
to learn syntax emphasizes readability and therefore reduces the cost of program
maintenance. Python supports modules and packages, which encourages
program modularity and code reuse. The Python interpreter and the extensive
standard library are available in source or binary form without charge for all
major platforms, and can be freely distributed.
Often, programmers fall in love with Python because of the increased
productivity it provides. Since there is no compilation step, the edit-test-debug
cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input
will never cause a segmentation fault. Instead, when the interpreter discovers an
error, it raises an exception. When the program doesn't catch the exception, the
interpreter prints a stack trace. A source level debugger allows inspection of
local and global variables, evaluation of arbitrary expressions, setting
breakpoints, stepping through the code a line at a time, and so on. The debugger
2
is written in Python itself, testifying to Python's introspective power. On the
other hand, often the quickest way to debug a program is to add a few print
statements to the source: the fast edit-test-debug cycle makes this simple
approach very effective.
ABOUT PROJECT
The logic of Vending Machine
Machine(), the primary function of the Python program, is written in the
vending machine. The three parameters this function will accept are the
items_data dictionary, the run variable with a boolean value, and the item
list, which includes all the items the user desires. A while loop is used,
however, it only functions when the run variable's value is True.
The product id of the desired item must be entered here. If the product id is
less than the total length of items_data dictionary, the entire set of id
properties must be added to the list of the item; otherwise, the message
"Wrong Product ID" will be printed. If the user declines, the run variable
3
will change to False and they will be prompted to add more items. A prompt
will ask if you want to print the entire bill or just the total sum
4
WORKING DESCRIPTION
Introduction to Coding a Vending Machine | In this lesson, we will learn
how to code a basic vending machine. This will involve understanding the
logic behind a vending machine's operation and translating it into code. We
will use a simple programming language for this lesson, but the concepts
can be applied to any language.
Understanding Vending Machine Operations | Before we start coding, it's
important to understand how a vending machine works. A vending machine
has a selection of items, each with a specific price. The user inserts money,
selects an item, and if they've inserted enough money, the item is dispensed
and any change is returned.
Setting Up Our Code | We'll start by setting up our code. This will involve
creating a list of items and their prices, and setting up variables to keep track
of the inserted money and selected item.
|Coding the Item Selection | The next step is to code the item selection. This
will involve creating a function that allows the user to select an item and
checks if the item is available.
| Coding the Money Insertion | After the item selection, we'll code the
money insertion. This will involve creating a function that allows the user to
insert money and keeps track of the total inserted money.
Coding the Item Dispensing | Once the item has been selected and the
money has been inserted, we'll code the item dispensing. This will involve
creating a function that checks if enough money has been inserted and, if so,
dispenses the item.
Coding the Change Return | The final step in coding our vending machine is
to code the change return. This will involve creating a function that
calculates the change to be returned and returns it to the user.
Testing Our Vending Machine Code | After we've coded our vending
machine, it's important to test it to make sure it works as expected. We'll go
5
through how to test each function and what to look for to ensure it's working
correctly.
Troubleshooting Common Issues | As with any coding project, you may run
into issues along the way. We'll go over some common issues you might
encounter when coding a vending machine and how to troubleshoot them.
Conclusion and Next Steps | In this lesson, we've learned how to code a
basic vending machine. But there's always more to learn! We'll discuss next
steps for expanding your knowledge and improving your vending machine
code.
6
SOURCE CODE
items_data = [
"itemId": 0,
"itemName": "Dairy Milk",
'itemPrice': 120,
},{
"itemId": 1,
"itemName": "5Star",
'itemPrice': 30,
},{
"itemId": 2,
"itemName": "perk",
'itemPrice': 50,
},{
"itemId": 3,
"itemName": "Burger",
'itemPrice': 200,
},{
"itemId": 4,
"itemName": "Pizza",
'itemPrice': 300,
},]
7
item = []
reciept = """
\t\tPRODUCT NAME -- COST
"""
sum = 0
run = True
print("------- Vending Machine Program with Python-------\n\n")
print("----------The Items In Stock Are----------\n\n")
for i in items_data:
print(
f"Item: {i['itemName']} --- Price: {i['itemPrice']} --- Item ID: {i['itemId']}")
def vendingMachine(items_data, run, item):
while run:
buyItem = int(
input("\n\nEnter the item code for the item you want to buy: "))
if buyItem < len(items_data):
item.append(items_data[buyItem])
else:
print("THE PRODUCT ID IS WRONG!")
moreItems = str(
8
input("type any key to add more things, and type q to stop: "))
if moreItems == "q":
run = False
receiptValue = int(
input(("1. Print the bill? 2. Only print the total sum: ")))
if receiptValue == 1:
print(createReceipt(item, reciept))
elif receiptValue == 2:
print(sumItem(item))
else:
print("INVALID")
def sumItem(item):
sumItems = 0
for i in item:
sumItems += i["itemPrice"]
return sumItems
def createReceipt(item, reciept):
for i in item:
reciept += f"""
\t{i["itemName"]} -- {i['itemPrice']}
"""
reciept += f"""
9
\tTotal --- {sumItem(item)}
"""
return reciept
# Main Code
vendingMachine(items_data, run, item)
INPUT/OUTPUT CONSOLE
10
11
12