The Shopping Cart Program sub-module programming, shopping cart 1 wrote the login section, the following rewrite the shopping section:
#!/usr/bin/env python
Product_list = [[' Iphone7 ', 5800],
[' Coffee ', 30],
[' Tea ', 10],
[' Python book ', 99],
[' Bike ', 199],
[' ViVo X9 ', 2499]]
Shopping_cart = {}
Username = input ("Username:")
Print ("****************************")
Password = input ("Password:")
Print ("****************************")
i = 1
While I <= 3: #登录判断
If username = = "Maomao" and password = = "123":
Print ("Welecom to shopping maket")
Break
elif i = = 3:
Print ("Input than three times, try again after 2 hours")
Break
Else
Print ("Put an wrong name or password,please input again")
i + = 1
Username = input ("Username:")
Password = input ("Password:")
Print ("*************** system operating Instructions ****************")
Print ("Q: Exit System")
Print ("C: Account Recharge")
Print ("H: Shopping History")
Print ("L: Product List")
Print ("************************************************")
Salary = 0
While True: #充值判断
temp = input ("You are zear,please rechange money to buy:")
If Temp.isdigit ():
salary = Int (temp)
Break
Print ("You input a unknow number,please check it and input again!")
While True:
index = 0
For product in Product_list: #打印商品列表, add the serial number before the product, just select the serial number when shopping
Print (index,product)
Index + = 1
Choice = str (input (">>:")). Strip ()
If Choice.isdigit (): #判断输入是否为数据
Choice = Int (choice)
If choice >= 0 and Choice < Len (product_list): #所选数字是否在商品序号范围内
Product = Product_list[choice]
If product[1] <= salary: #账户所剩金额是否够支付所选商品
If product[0] in Shopping_cart: #同一种商品, multiple purchases, to achieve the number of goods stacked
SHOPPING_CART[PRODUCT[0]][1] + = 1
Else
Shopping_cart[product[0]] = [product[1], 1]
Salary-= product[1]
Print ("\033[42;1madded product:" + product[0] + "into shoing cart, your current balance"
+ STR (Salary) + "\033[0m")
else: #账户余额不足提醒
Print ("\033[42;1m [WARNING] You balance are no enough\033[0m, product:" +str (product[1]) + "short for"
+ str (product[1]-salary))
Else
Print ("You choice product was no in product_list")
elif Choice = = "Q": #退出
Print ("\033[34;1m---------------Product List-----------------\033[0m")
Print ("\033[42;1m")
Print ("ID Product number pric totalpric")
index = 1
Total_mon = 0
f = open ("Product.txt", "A +", encoding= "UTF8") #因为要记录多次购物记录, so this opens the mode for append write
Import datetime
For I in Shopping_cart.keys (): #打印购物列表, and print the number of items, as well as the amount
Total_mon + = shopping_cart[i][1] * Shopping_cart[i][0]
Print ("%d%10s%7d%7d%7d"% (Index,i,shopping_cart[i][1], shopping_cart[i][0],
SHOPPING_CART[I][1] *shopping_cart[i][0]))
If index = = 1:
Now_time = Datetime.datetime.now (). strftime (' [%y-%m-%d] ')
F.write (now_time + "\ n")
F.write (i+ "%7d"%+shopping_cart[i][0]+ "\ n") #保存购物列表
F.write (i + "\ n")
Index + = 1
F.close ()
Print ("You pay money is:", Total_mon)
Print ("You level money is:", salary)
Print ("\033[0m")
Print ("\033[34;1m-------------end-------------\033[0m")
Break
elif Choice = = "C": #充钱
Salary + = Int (input ("Please input recharge:"))
Print ("Current account balance:", salary)
Continue
elif Choice = = ' h ':
f = open ("Product.txt", "r+", encoding= "UTF8")
For I in F:
Print (I.strip ())
Continue
Else
Print ("You input wrong oper,please input again!")
Python Learning-Shopping cart 2