Python basic exercise three supermarket storage cabinet simulation (optimization)

Title Description Simulate the operation of storing and taking out the items in the supermarket's storage cabinet. When storing the items, select...
Title Description

Simulate the operation of storing and taking out the items in the supermarket's storage cabinet. When storing the items, select the empty storage box, and then assign the password to complete the storage. When taking out the items, input the corresponding password, and open the corresponding door to take out the items

Title Analysis

1. Add private variables and practice the use of private variables
2. Many data types need to be converted, such as int(),str(), etc
3. String splicing and truncation. Slicing is used for truncation
4. The way of random password + location code is adopted in the password, which can locate the location of the box directly according to the password and avoid the problem of repeated passwords generated randomly

Code

This time, we mainly optimized the check ﹐ cell() method, save ﹐ goods() method and get ﹐ goods ﹐ out() method. The get ﹐ goods ﹐ out() method changed the most, cancelled the for loop, and improved the search performance.

#!/usr/bin/python3 #-*- coding:UTF-8 -*- import random ''' //Simulate the supermarket storage cabinet program, set up 100 boxes, and store them when they are full //Before each storage, check whether the first box is empty. If the first box is empty, the items can be stored ''' class Locker(object): def __init__(self): self._cell_num = 100 self._use = 0 self._surplus = self._cell_num self.cell = [0]*100 def show_cell_detail(self): print(f"\n####################################\n total number of packets: total lattice, used lattice, remaining lattice ") def get_surplus(self): return self._surplus def check_cell(self): for i in range(0,100): position = "%02d"%(i) #Format the box position into 2 digits, and fill in 0 for the insufficient ones if self.cell[i] == 0: return position return -1 #Storage method: first use the inspection method to check whether there is an empty box, and then open the door for storage def save_goods(self): self.passwd = random.randint(10000,99999) self.cell_save = self.check_cell() self.passwd = str(self.passwd) + self.cell_save #The last two digits of the password represent the location of the box. At the same time, the problem of repeated password can be avoided self.cell_save = int(self.cell_save) self.cell[self.cell_save] = self.passwd print(self.cell[self.cell_save]) self._use += 1 self._surplus = self._cell_num - self._use print(f"The door of No. 1 box has been opened. Your password is") #Method of taking out items, input password and take out items def get_goods_out(self,password): password = str(password) i = int(password[5:7]) #Directly take the last two digits of the password to locate the location of the box to avoid the time consumption of cyclic search if self.cell[i] == password: print(password) self.cell[i] = 0 self._surplus += 1 self._use = self._cell_num - self._surplus return i return -1 lock = Locker() #lock.check_cell() while True: lock.show_cell_detail() surplus = lock.get_surplus() operation = input("1-Deposit\n2-take out\n0-Sign out\n Please enter the corresponding operation:") if operation == "1": if surplus != 0: lock.save_goods() else: print("Thank you for using") break elif operation == "2": while True: password = input("Please enter the access code:") password = int(password) out_result = lock.get_goods_out(password) if out_result != -1: print(f"The No. box door has been opened and the password has expired. Please take out the goods and close the box door") break else: print("The password is wrong. Please check it before entering!") elif operation == "0": print("Looking forward to your next visit!") break else: print("Please input the correct operation!") continue

3 December 2019, 12:47 | Views: 1816

Add new comment

For adding a comment, please log in
or create account

0 comments