第二周作业-购物车

需求:

购物车程序:

1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表

2、允许用户根据商品编号购买商品

3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒

4、可随时退出,退出时,打印已购买商品和余额

5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买

7、允许查询之前的消费记录

程序实现

1.逻辑图

2.主程序

博客地址:http://www.cnblogs.com/Mr-hu/

程序运行步骤:
第1步:根据提示输入用户名,如果用户名和密码正确且第1次或第3次登陆,提示输入salary或搜索历史购物记录
      重复3次不正确后,提示用户名被锁定,即使下次登录,还是被锁定,该用户名被锁定同时写入lock文件。
第2步:根据列表选择商品编号进行购买,余额足够,直接扣款,不够提示并重新输入
第3步:如果退出会进行结算和显示余额,并把商品和余额信息写入cart_list数据库
第4步:如果第2次登陆,会直接显示上次的购买清单及余额

本程序u_database中对登陆次数的修改还不够完善,现在需要人工修改0.1.2分别代表第1,2,3次登陆。
readme.txt
import sys,os

def login():
    count = 0
    while count <3:
       usr = input("Pls input your username:")
       with open("u_database", "r+") as f,open("lock", "r+") as f2:
           lock_list = f2.readlines()
           for lock_line in lock_list:
               lock_line = lock_line.strip('\n')
               if usr == lock_line:
                  sys.exit('user %s has been locked,pls contact the admin!' % usr)
           user_list = f.readlines()
           for user_line in user_list:
               user_line1 = user_line.strip("\n").split(",")
               username,password,history = user_line1
               if usr == username:
                  pwd = input("Pls input your password:")
                  if pwd == password:
                     if int(history) == 0:
                         history =int(history) +1
                         user_line1[2] = str(history)
                         new_user_line = ",".join(user_line1)
                         f.writelines(new_user_line+"\n")
                         return 1
                     elif int(history) == 1:
                         history = int(history) + 1
                         user_line1[2] = str(history)
                         new_user_line = ",".join(user_line1)
                         f.writelines(new_user_line+"\n")
                         return 2
                     elif int(history) >= 2:
                         history = int(history) + 1
                         user_line1[2] = str(history)
                         new_user_line = ",".join(user_line1)
                         f.writelines(new_user_line+"\n")
                         return 3
                  else:
                     print("Your username or password not correct!")
                     count += 1
                     print("count:", count)
                     continue
    else:
       print("user %s has been locked,pls contact the admin!" %usr)
       with open("lock", "r+") as f2:
           f2.write(usr+"\n")
           return 0
def search():
    with open("cart_list", "r+") as s:
        search_result = s.read()
        print(search_result)



x = login()
if x == 1 or x ==3:
    salary = input("Pls input your salary or check deals before with 's' :")
    if salary.isdigit():
       salary = int(salary)
    elif salary == "s":
         search()
         pass

    product_list = [['IPhone', 8800], ['Mac Pro', 12388], ['CoCo', 44], ['Mouse', 181], ['Bike', 800]]
    count = 0
    for list in product_list:
        item,price = list
        count +=1
        print(str(count)+'.'+item,price)


    with open("cart_list","r+") as s:
     shopping_list = []
     shopping_list2 = []
     balance_item =[]
     while True:
        user_choice = input('Pls input you need what to buy with number:')
        if user_choice.isdigit():
           user_choice = int(user_choice)
           if user_choice < len(product_list)+1 and user_choice > 0:
              buy_list = product_list[(int(user_choice) - 1)]
              if salary - buy_list[1] > 0:
                 new_item = product_list[product_list.index(buy_list)][0]
                 salary -= buy_list[1]
                 print('added [%s] to your shopping cart,and your current balance is \033[31;1m%s\033[0m'%(new_item,salary))
                 shopping_list.append(buy_list)
              else:
                 print('Your balance not enough')
           else:
               print("product code [%s] is not exist"%user_choice)
        elif user_choice == "q":
            print("have bought below:")
            for p in shopping_list:
                print(p)
                for q in p:
                   shopping_list2.append(str(q))
            print("Your balance:",salary)
            list_str = ",".join(shopping_list2)
            s.writelines(list_str+ "\n")
            s.writelines("Your balance is:"+ str(salary) + "\n")
            break
        else:
            print("Invalid syntax !")
elif x== 2:
    with open("cart_list","r") as r:
       result = r.read()
       print(result)
main.py
huwei,123123,2
u_database
默认为空
lock.txt
默认为空
cart_list.txt

3.程序运行

 

 

posted @ 2017-07-30 19:43  Mr.hu  阅读(107)  评论(0编辑  收藏  举报