Python Shopping Cart Program
Data structure: goods = [{"Name": "Computer", "Price": 1999},{"name": "Mouse", "Price": 10},{"name": "Yacht", "Price": 20},{"name": "Beauty", "price" : 998},......] Functional Requirements: Basic requirements: 1, after the start of the program, enter the user name password, let the user enter the salary, and then print the Product List 2, allow the user to purchase goods according to the product number 3, the user selects the goods, the detection balance is enough, enough on the direct debit, enough on the reminder 4, can withdraw at any time, Print purchased goods and balances 5, in the user process, the key output, such as the balance, the product has been added to the shopping cart and other messages, need to highlight the expansion needs: 1, the user after the next login, enter the user name password, directly back to the last state, that is the last consumption of the balance or those, re-login to continue to Allow query of previous consumption records
1 #!-*-coding:utf-8-*-2 #Shopping Cart Procedure3 4Goods = [5{"name":"Computer"," Price": 1999},6{"name":"Mouse"," Price": 10},7{"name":"Yacht"," Price": 20},8{"name":"Beauty"," Price": 998},9 ]Tendata = {} One whileTrue: AUsername = input ("User name:") -Password = input ("Password:") -n = open ("Data.txt","A +")#Open File the n.seek (0, 0) -D =N.read () - n.close () - ifD = ="": +D + ="{}" -data = eval (d)#String to Dictionary + ifUsernameinchData#determine if the user exists A ifPassword = = Data[username][0]:#Determine if the password is correct atWages_1 = data[username][1]#Read Balance - Print("Login Successful!") -F = Username +". txt" -Record = open (f,"A +")#Open a consumer record file - record.seek (0, 0) -consume = Record.read ()#Consumption record inRecord.seek (0, 2) - Break to Else: + Print("wrong password!") - Enumerate the Else: *Wages = input ("Salary:") $ ifwages.isdigit ():Panax Notoginsengwages_1 = Int (wages)#Deposit Balance -Data[username] = [Password, wages_1]#Add user information, hex encryption password theF = Username +". txt" +Record = open (f,"A +")#Open a consumer record file Aconsume ="" the Break + Else: - Print("you entered the wrong salary, please re-enter!") $ $LIST_0 = []#Store all Items -List_1 = []#Store purchased Items - forInfoinchGoods: theList_0.append ([info['name'], info[' Price']])#Add product information to the list - whileTrue:Wuyi Print('-'* 7,"Product List",'-'* 7) the forIndex, pinchEnumerate (LIST_0): - Print("%s.%s%s"% (index, p[0], p[1]))#print a list of items Wu Print("Please enter the product number below, or N to view the consumption record, or q/q exit \ n",'-'* 26) -select = Input (">>>") About ifselect = ="Q" orselect = ="Q" orselect = ="N": $ ifselect = ="N": - Print('-'* 7,"Consumption record",'-'* 7) - Print(consume) - Break A Else: + Print('-'* 6,"List of Purchased items",'-'* 6) the forIndex_1, P_1inchEnumerate (list_1): - Print("%s.%s%s"% (Index_1, p_1[0], p_1[1]))#Print List of purchased items $c =P_1[0] theConsume + =Str (c) theConsume + ="\ n" the Print("Balance:", Wages_1,"\ n",'-'* 26) thenn = open ("Data.txt","W") -DD = str (data)#Dictionary to String in Nn.write (DD) theNn.close ()#Close File theRecord.write (Consume)#write a consumption record Aboutconsume ="" the record.close () the Break the Else: + ifSelect.isdigit ():#determine if it is a number -select =int (SELECT) the ifSelect <= 3:BayiPrice = List_0[select][1]#get the corresponding commodity price with the Select index the ifWages_1 >= Price:#determine if the balance is greater than or equal to the commodity price theWages_1 = Wages_1-price#Deduct Money -List_1.append (List_0[select])#add purchased items to the list of purchased items -DATA[USERNAME][1] = Wages_1#Modify Balance the Print("Purchase Success!") the Else: the Print("Insufficient balance!") the Else: - Print("without this option, please re-enter!") the Else: the Print("without this option, please re-enter!")
Python Shopping Cart Program