반응형
Python의 Dictionary를 이용해서 간단한 POS 앱을 작성합니다.
메뉴는 다음과 같이 구성됩니다.
----------Welcome to the supermarket--------------
1. 상품조회 View items
2. 상품추가 Add items
3. 상품구매 Purchase items
4. 상품검색 Search items
5. 상품변경 Edit items
6. 나가기 Exit
Python 코드는 다음과 같습니다.
items=[]
while True:
print("----------Welcome to the supermarket--------------")
print("1. 상품조회 View items \n2. 상품추가 Add items\n3. 상품구매 Purchase items\n4. 상품검색 Search items\n5. 상품변경 Edit items\n6. 나가기 Exit ")
choice=int(input("Enter the number of your choice: "))
if choice == 1:
print("------View Items------")
print("The number of items in the inventory are: %d" %len(items))
if len(items) != 0:
print("Here are all the items available in the supermarket.")
for item in items:
for key,value in item.items():
print("%s : %s" %(key, value))
elif choice == 2:
print("-----Add items------")
print("To add an item fill in the form")
item = {}
item["name"] = input("Item name: ")
while True:
try:
item["quantity"] = int(input("Enter the number of quantity: "))
break
except ValueError:
print("Quantity should only be in digits")
while True:
try:
item["price"] = int(input("Enter item price: "))
break
except ValueError:
print("Price should only be in digits")
print("Item has been successfully added.")
items.append(item)
elif choice == 3:
print("-----Purchase items------")
print(items)
purchase_item = input("Which item do you want to purchase? Enter name: ")
purchase_quantity = int(input("Enter the quantity needed: "))
for item in items:
if purchase_item.lower() == item["name"].lower():
if item["quantity"] != 0:
if purchase_quantity <= item["quantity"]:
print("Pay %d at checkout counter" %(item["price"]* purchase_quantity))
item["quantity"] -= purchase_quantity
else:
print("Quantity required is not available")
else:
print("Item out of stock.")
elif choice == 4:
print("-----Search items-----")
find_item = input("Enter the item name to search in the inventory: ")
for item in items:
if item["name"].lower() == find_item.lower():
print("The item named " + find_item + "is displayed below with is details")
print(item)
else:
print("item not found")
elif choice == 5:
print("------Edit items-----")
item_name = input("Enter the name of the item you want to edit: ")
for item in items:
if item_name.lower() == item["name"].lower():
print("Here are the current details of " + item_name)
print(item)
item["name"] = input("Item name: ")
while True:
try:
item["quantity"] = int(input("Enter item quantity: "))
break
except ValueError:
print("Quantity should only be in digits")
while True:
try:
item["price"] = int(input("Enter item price: "))
break
except ValueError:
print("Price should only be in digits")
print("Item has been updated")
print(item)
else:
print("Item not found")
elif choice == 6:
print("-----Exited-----")
break
else:
print("You entered invalid option.")
실행화면 입니다.
반응형
'PROGRAMING > Python' 카테고리의 다른 글
Formatting Strings (0) | 2024.08.23 |
---|---|
Python resources (5) | 2024.06.17 |