This is a basic command line app that interactively collect the information from the user and process it. Previously I thought this kind of app is not for users, but sometimes I wonder actually this is better for users in some cases. I should learn more of this kind of approach.
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
$ python3 inventory.py ### Room Inventory App ### How many rooms do you have?: 2 please tell me about the #1 room. What do you call this name?: Bed room What do you want to do? (A)dd/(R)emove/(N)ext: A What do you want to add?: Double Bed What do you want to do? (A)dd/(R)emove/(N)ext: A What do you want to add?: Pillow What do you want to do? (A)dd/(R)emove/(N)ext: Pillow What do you want to do? (A)dd/(R)emove/(N)ext: N please tell me about the #2 room. What do you call this name?: Reception What do you want to do? (A)dd/(R)emove/(N)ext: A What do you want to add?: Table What do you want to do? (A)dd/(R)emove/(N)ext: A What do you want to add?: Chair What do you want to do? (A)dd/(R)emove/(N)ext: A What do you want to add?: Chair What do you want to do? (A)dd/(R)emove/(N)ext: N Thank you for your input. All data has been collected. What do you want to do?: (P)rint/(S)ave/(Q)uit: P ^-^-^-^-^-Bed room-^-^-^-^-^- Double Bed QTy:1 Pillow QTy:1 ------------------------------ ^-^-^-^-^-Reception-^-^-^-^-^- Table QTy:1 Chair QTy:2 ------------------------------ What do you want to do?: (P)rint/(S)ave/(Q)uit: Q |
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from collections import defaultdict import sys class Room(object): def __init__(self, name): self.name = name self.items = defaultdict(int) def add_item(self, item): self.items[item] += 1 def remove_item(self, item): if self.items.get(item, 0) > 0: self.items[item] -= 1 return True else: return False def __len__(self): return len(items) def display_items(items): for item in items: if items[item] > 0: print(f"{item}\tQTy:{items[item]}") if __name__ == "__main__": print("### Room Inventory App ###") rooms = [] nos = input("How many rooms do you have?: ") for no in range(1, int(nos)+1): print(f"\nplease tell me about the #{no} room.") name = input("What do you call this name?: ") room = Room(name) while True: operation = input("What do you want to do? (A)dd/(R)emove/(N)ext: ") if operation == 'A': item = input("What do you want to add?: ") room.add_item(item) elif operation == "R": item = input("What do you want to remove?: ") room.remove_item(item) elif operation == "N": break rooms.append(room) print("\nThank you for your input. All data has been collected.") while True: operation = input("What do you want to do?: (P)rint/(S)ave/(Q)uit: ") if operation == "P": for room in rooms: print(f"^-^-^-^-^-{room.name}-^-^-^-^-^-") display_items(room.items) print(f"------------------------------") if operation == "S": pass if operation == "Q": sys.exit(0) |