Shopping cart Program Job requirements:
1, after starting the program, enter the user name password, if it is the first time to log in, let the user enter wages, and then print the product list
2. Allow users to purchase goods according to the product number
3, the user selects the goods, checks whether the balance is enough, enough on the direct debit, not enough on the reminder
4, can withdraw at any time, exit, print purchased goods and balance
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
6, the user after the next login, enter the user name password, directly back to the last state, that is, the balance of the last consumption or those, re-login to continue to purchase
7, allow to query the previous consumption records
The code is as follows:
#AUTHOR:PENGP#!/usr/bin/env python#-*-coding:utf-8-*-With Open ("user_shopping","R", encoding="Utf-8") as F:yj=eval (F.read ()) whileTrue:username= Input ("Please enter user name: \033[0m") passwd= Input ("Please enter the password: \033[0m") ifUsername = = Yj["name"] andpasswd = = yj["passwd"]: Print("your balance of funds is: \033[1;31;0m%s\033[0m"% (yj["Salary"])) Else: Print("incorrect user name or password") exit () whileTrue:pay= Input ("Would you like to recharge it? (y/n):") ifPay = ="y": PAY_RMB= Input ("Please enter the amount you want to recharge:") Salary_new= yj["Salary"] +Int (PAY_RMB) yj["Salary"] =salary_new#yj_pay_salary = (yj["Salary"]) Print("the balance is now \033[1;31;0m%d\033[0m"% (yj["Salary"])) with open ("user_shopping","W", encoding="Utf-8") as Ff:ff.write (str (YJ))elifPay = ="N": F= Open ('product_list','R') Product_list=F.readlines () whileTrue:Print("product_list". Center (50,'-')) forIndex,iteminchEnumerate (product_list): Item=Item.split ()Print(index,item) User_choice= Input ("Please select purchase product code, exit please press E:") ifuser_choice.isdigit (): User_choice=Int (user_choice)ifUser_choice <Len (product_list) andUser_choice >=0:p_item=Product_list[user_choice].split ()ifInt (p_item[1]) <yj["Salary"]: salary_banance= yj["Salary"]-Int (p_item[1]) yj["Salary"] =salary_banance yj["shopping_list"].append (P_item) with open ("user_shopping","W", encoding="Utf-8") as F_new:f_new.write (str (YJ))Print("Product%s has been purchased"%P_item)Else: Print("insufficient balance, please recharge first") Break Else: Print("Product number%s does not exist, please re-enter:"%User_choice)Continue elifUser_choice = ='e': Print('shopping_list'. Center (50,'-')) forIndex,pinchEnumerate (yj["shopping_list"]): Print(index,p) exit ("your balance is:%s, welcome to the next visit"% (yj["Salary"]))
17.python Shopping Cart Program job