购物车程序(我都觉得丑的代码):包含商家接口和买家接口

购物车要求:
顾客接口:
 1、读取商品列表(从文件中读取);
 2、新用户输入工资,如果老用户,记录上次余额;
商家接口:
 1、增加商品;
 2、修改商品价格。
1 #商品文件内容
2 iphone 5800
3 coffee 50
4 watch 3000
5 car 300000
6 duck 45
7 fish 6
8 women 7000
1 #用户名、密码、余额文件
2 yb 1234 5000
3 bigd 1234 12000
4 jack 123456 8000
  1 #Author BigD
  2 # -*- coding:utf-8 -*-
  3 
  4 # 购物车要求:
  5 #     顾客接口:
  6 #         1、读取商品列表(从文件中读取);
  7 #         2、新用户输入工资,如何不是,记录上次余额
  8 #     商家接口:
  9 #         1、增加商品;
 10 #         2、修改商品价格
 11 
 12 def show_menu():
 13     shop_list = []
 14     shop_menu_file = open("shop_menu", "r")
 15     for shop_line in shop_menu_file:  # 遍历将内容写入列表
 16         shop_list.append(shop_line.strip().split(' '))
 17     # print(shop_list)
 18     print("shopping list".center(40, "="))
 19     for i in shop_list:  # 展示商品信息
 20         print(i[0], i[1])
 21     # print(shop_list)
 22     return shop_list
 23 
 24 def write_shop_menu():
 25     shop_menu_ok = ""
 26     for shop_list_line in shop_list:
 27         line = str(shop_list_line[0]) + " " + str(shop_list_line[1]) + "
"
 28         shop_menu_ok += line
 29     open("shop_menu", "w").write(shop_menu_ok)
 30 
 31 while True:
 32     use_type = input("who are you ?boss(b) or customer(c)?")
 33     if use_type == "b":
 34         print("welcome BOSS!")
 35         while True:
 36             shop_list = show_menu()
 37             # print(shop_list)
 38             boss_choice = input("Do you want add(a) product or modify(m) price?")
 39             if boss_choice == "a":
 40                 product_name = input("enter the product name?")
 41                 product_price = input("enter the price?")
 42                 shop_list.append([product_name,product_price])
 43                 write_shop_menu()
 44             elif boss_choice == "m":
 45                 change_product = input("which product do you want to modify?")
 46                 change_price = input("what price do you want to change?")
 47                 for i in shop_list:
 48                     if i[0] == change_product:
 49                         i[1] = change_price
 50                 # show_menu = show_menu()
 51                 write_shop_menu()
 52             else:
 53                 print("wrong!")
 54 
 55     elif use_type == "c":
 56         print("wecome CUSTOMER
")
 57         while True:
 58             user_list = []
 59             for user in open("user_menu","r"):
 60                 user_list.append(user.strip().split(" "))
 61             print(user_list)
 62 
 63             g_name = input("what is your name ?")
 64             salary = ""
 65 
 66             for p in user_list:
 67                 if p[0] == g_name:
 68                     name = g_name
 69                     passwd_count = 3
 70                     while True:
 71 
 72                         if passwd_count > 0:
 73 
 74                             passwd = input("what is your password?")
 75                             if passwd == p[1]:
 76                                 salary = int(p[2])
 77                                 print("your salary is %d" %salary)
 78                                 break
 79                             else:
 80                                 passwd_count -= 1
 81                                 print("your password is wrong,you have %d times to try." % passwd_count)
 82 
 83                         else:
 84                             exit()
 85                 else:
 86                     pass
 87 
 88             if salary == "":
 89                 name = g_name
 90                 salary = int(input("you are first come ,enter your salary."))
 91 
 92 
 93             # print(name)
 94             # print(salary)
 95 
 96             ok_menu = []
 97             while True:
 98                 shop_list = show_menu()
 99                 choose_product = input("you have $%s,what do you want to buy?" % salary)
100 
101                 for i in shop_list:
102                     if i[0] == choose_product:
103                         if int(i[1]) < salary:
104                             salary -= int(i[1])
105                             ok_menu.append(i[0])
106                             print(ok_menu)
107                         else:
108                             print("余额不足,请重新选择")
109                 if choose_product == "q":
110                     print(ok_menu)
111                     exit("bye")
112 
113 
114     else:
115         print("wrong choice.")

技术还是欠缺,写写停停,耽误了挺久,写的不好,看客莫介意,功能肯定还是欠缺的,看了一点函数,优化了一点,就到这儿吧,后面再完善补全。

原文地址:https://www.cnblogs.com/dabingya/p/6232714.html