购物车: 用户启动程序后打印商品列表 允许用户选择购买商品 允许用户不断地购买各种商品 购买时检测 余额是否足够,如果足够,直接扣款 否则打印余额不足 允许用户主动退出程序,退出时打印已购商品列表
作业需求: 1.优化购物程序,购买时允许用户选择购买多少件 2.允许多用户登陆,下一次登录后,继续按上次的余额继续购买(可以充值) 3.允许多户查看之间的购买记录(记录要显示商品购买时间) 4.商品列表分级展示,比如 第一层菜单: 1.家电类 2.衣服 3.手机类 4.车类
随便选择一个,比如车类,进入第2层 1.BMW X3 300000 2.AUDI Q5 350000 3.pasate 333335 4 tesla model_3 430000 5.tesla model_s 8888888
5.显示已购买商品时,如果有重复的商品,不打印多行,而是在一行展示
id p_name num total_price
1. TeslaModelS 2 35000000
2. coffee 4 10000
# Author:Tim Gu import json #====================================================用户登录模块============================================================== users = json.load(open('user.txt','r')) #从文件读取用户信息(用户名,密码,账户余额) counter = 0 while counter < 3: username = input('username:') # 输入用户名 password = input('password:') #输入密码 if username in users.keys() and password == users[username][0]: #判断用户名和密码是否匹配 print("You have login!") break else: print("Invaild username or password!please try again!") counter += 1 if counter == 3: print('Try too many times!Bye!') exit() welcome_msg = 'Welcome to Tim Shopping mall'.center(50, '-') print(welcome_msg) print("Your balance is %d"%users[username][1]) #============================================================================================================ #============================================用户充值模块======================================================== flag = 0 while flag < 3: ifrecharge = input("Do you want to recharge your salary? 'y' or 'n' or 'q'") if ifrecharge == 'n': break elif ifrecharge == 'y': for i in range(3): add = input("How much do you to fill?") if add.isdigit() and int(add) > 0: users[username][1] += int(add) json.dump(users, open('user.txt', 'w')) # 充值成功的更新文件内的余额 break else: if i < 2: print("Your input is wrong,Please try again!") elif i == 2: print("You input wrong!Bye!") exit() continue flag = 3 print("Your current balance is %d!"%users[username][1]) elif ifrecharge == 'q': print("ByeBye!") json.dump(users, open('user.txt', 'w')) exit() else: flag += 1 if flag < 3: print("Your input is wrong,Please try again!") else: print("You try too many time,Bye!") exit() #====================================================================================================================== #=============================================打印商品列表============================================= exit_flag = False product_list = [ ('Iphone',5888), ('Mac Air',8000), ('mac pro',9000), ('xiaomi 2',120), ('Coffer',30), ('Tesla',820000), ('Bike',700), ('Cloth',200) ] salary = users[username][1] #定义变量余额 shop_car = [] while exit_flag != True: print('product list'.center(50,'-')) for item in enumerate(product_list):#枚举元组 index = item[0] p_name = item[1][0] p_price = item[1][1] print(index,p_name,p_price) #=============================================================================================================== user_choice = input('[q=quit,c=check]What do you watn to buy?:') if user_choice.isdigit():#肯定是选择商品 #------------------------------------------选择商品数量--------------------------------------------------------------- count2 = 0 while count2 < 3: buy_sum = input("How many do you want to buy?") if buy_sum.isdigit() and int(buy_sum) > 0: buy_sum = int(buy_sum) break else: count2 += 1 if count2 == 3: print("You try too many times,Bye!") exit() else: print("Please input a number of the goods!") #--------------------------------------------------------------------------------------------------------------------- user_choice = int(user_choice) if user_choice < len(product_list): p_item = product_list[user_choice] if p_item[1] * buy_sum <= salary: #资金够买 shop_car.append(product_list[user_choice]) salary -= (p_item[1] * buy_sum) users[username][1] = salary print('Added 33[31;1m[%s]* %d 33[0m into shop car,you current balance is 33[31;1m[%d] 33[0m'%( p_item[0],buy_sum,salary)) json.dump(users, open('user.txt', 'w')) else: print("Your balance is [%s],cannot afford this..."%salary) elif user_choice == 'q' or user_choice == 'quit': print("purchased products as below".center(40,'*')) for item in shop_car: print(item) print("END".center(40,'*')) print("Your balance is [%s] last"%salary) exit_flag = True elif user_choice == 'c' or user_choice == 'check': print("purchased products as below".center(40, '*')) print("-序号---商品----数量----总价-------") for k, v in enumerate(shop_car): print("%d %s %d %d"%(k,v[0],buy_sum,v[1]*buy_sum)) print("END".center(40, '*')) print("Your balance is 33[41;1m[%s] last 33[0m" % salary)