Python 编写自动售货机
在这篇文章中,我们将学习用Python编码一个自动售货机。
用Python编写自动售货机
每个物品的产品ID、产品名称和产品成本属性都将存储在一个字典中。一个列表目前是空的,但以后会被所有选定的物品填满。
一个 “运行 “变量的值是 “真”,直到用户决定他们已经满意并且不想再购买任何产品时为止;这时,该值被改为 “假”,并且循环结束。
我们现在将尝试理解自动售货机的Python代码。
items_data = [ { "itemId": 0, "itemName": "Dairymilk", 'itemCost': 120, },{ "itemId": 1, "itemName": "5star", 'itemCost': 30, },{ "itemId": 2, "itemName": "perk", 'itemCost': 50, },{ "itemId": 3, "itemName": "Burger", 'itemCost': 200, },{ "itemId": 4, "itemName": "Pizza", 'itemCost': 300, }, ] item = [] bill = """ \t\tPRODUCT -- COST """ sum = 0 run = True
打印菜单
写一个简单而直接的循环来打印自动售货机的菜单以及每个项目的必要属性
print("------- Vending Machine Program with Python-------\n\n") print("----------Items Data----------\n\n") for item in items_data: print(f"Item: {item['itemName']} --- Cost: {item['itemCost']} --- Item ID: {item['itemId']}")
计算总价
我们创建了一个名为sum()的函数,它遍历了所有被选中的购买项目的列表。在列表上执行一个循环,并将product_cost的属性添加到总数中后,该函数将返回总金额。
def sumItem(item): sumItems = 0 for i in item: sumItems += i["itemPrice"] return sumItems
自动售货机的逻辑
Machine(),Python程序的主要函数,写在自动售货机中。这个函数将接受的三个参数是 items_data 字典、带有布尔值的 run 变量和项目列表,其中包括用户想要的所有项目。使用了一个while循环,但是,它只在run变量的值为True时发挥作用。
必须在这里输入所需物品的产品ID。如果产品id小于 items_data字典 的总长度,整个id属性集必须被添加到物品列表中;否则,将打印出 “错误的产品ID “ 信息。如果用户拒绝,运行变量将变为False,并将提示他们添加更多的项目。一个提示将询问你是否要打印整个账单或只是总和。
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( 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")
创建账单的功能
在控制台创建账单显示是Python自动售货机的另一个特点。函数 create_bill() 将接受两个参数,即
- 所选产品的 项目 列表
-
账单, 这是一个由模板菜单组成的字符串,并且已经被选中。
在它遍历物品清单的过程中,物品的名称和价格被选中,必要的信息被打印出来。最后,这段代码将再次使用之前的 sum( )函数输出整个费用。
记住,这个 create_bill( )方法是在 sum( )函数之外独立创建的。
def create_bill(item, bill): for i in item: bill += f""" \t{i["itemName"]} -- {i['itemCost']} """ bill += f""" \tTotal Sum --- {sum(item)} """ return bill
Python自动售货机的完整代码
例子
下面是用Python创建自动售货机的所有步骤的完整代码。
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, }, ] 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( 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""" \tTotal --- {sumItem(item)} """ return reciept # Main Code vendingMachine(items_data, run, item)
输出
------- Vending Machine Program with Python------- ----------The Items In Stock Are---------- Item: Dairy Milk --- Price: 120 --- Item ID: 0 Item: 5Star --- Price: 30 --- Item ID: 1 Item: perk --- Price: 50 --- Item ID: 2 Item: Burger --- Price: 200 --- Item ID: 3 Item: Pizza --- Price: 300 --- Item ID: 4 Enter the item code for the item you want to buy: 2 type any key to add more things, and type q to stop: t Enter the item code for the item you want to buy: 3 type any key to add more things, and type q to stop: q 1. Print the bill? 2. Only print the total sum: 1 PRODUCT NAME -- COST perk -- 50 Burger -- 200 Total --- 250
结论
我们在本文中详细研究了如何用Python创建一个自动售货机程序以及主要逻辑是如何工作的。