博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
购物车练习
阅读量:4575 次
发布时间:2019-06-08

本文共 5359 字,大约阅读时间需要 17 分钟。

购物车: 用户启动程序后打印商品列表 允许用户选择购买商品 允许用户不断地购买各种商品 购买时检测 余额是否足够,如果足够,直接扣款 否则打印余额不足 允许用户主动退出程序,退出时打印已购商品列表

作业需求: 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 Guimport json#====================================================用户登录模块==============================================================users = json.load(open('user.txt','r')) #从文件读取用户信息(用户名,密码,账户余额)counter = 0while 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 = 0while 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 = Falseproduct_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 \033[31;1m[%s]* %d\033[0m into shop car,you current balance is \033[31;1m[%d]\033[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 \033[41;1m[%s] last\033[0m" % salary)

 

转载于:https://www.cnblogs.com/guqing/p/6079325.html

你可能感兴趣的文章
Java实现Windows锁屏
查看>>
在线会话管理
查看>>
文本处理之可视化wordcloud
查看>>
SampleManager(赛默飞)
查看>>
堆排序
查看>>
我的面试问题记录
查看>>
函数PARSENAME使用和截取字符串
查看>>
关乎性能的判断,请作出果断选择
查看>>
判断是否包含指定的字符
查看>>
[Html5] HTML5 开发手机应用
查看>>
[工具] 各种主流 SQLServer 迁移到 MySQL 工具对比
查看>>
(二)Maven 基本概念——依赖、生命周期、仓库管理、聚合&继承
查看>>
py4CV例子3Mnist识别和ANN
查看>>
【4Opencv】如何识别出轮廓准确的长和宽
查看>>
现货黄金交易计划摸索
查看>>
Django中国|Django中文社区——python、django爱好者交流社区
查看>>
java中的toArray()
查看>>
java数据库之JDBC
查看>>
C语言 strcpy,memcpy,memmove,memccpy函数
查看>>
SqlSession 内部运行
查看>>