首页 技术 正文
技术 2022年11月10日
0 收藏 552 点赞 3,359 浏览 2941 个字

1、购物车流程图:

2、代码实现:

#!/usr/bin/env python#coding=utf-8ChoiceOne ='''1.查看余额2.购物3.退出'''ChoiceTwo = '''1.商品列表2.已选择商品3.结算4.退出'''ShopList = [    ["iPhone 7 Plus",6388],    ["NBShoes",588],    ["MacBook",13800],    ["返回上一层",""],]#导入模块import sys,fileinputglobal UserNameglobal UserPassdef Login():    #设置尝试密码的次数为0    RetryNum = 0    #格式化输入的用户名    OldName = "Null"    #开始程序    while RetryNum < 3:        #输入用户信息        UserName = input("请输入用户名: ").strip()        UserPass = input("请输入密码: ").strip()        #打开用户信息文件        UserList = open("UserInfo","r")        #查看锁定的用户        UserLockF = open("LockUserFile","r")        #遍历锁定的用户有哪些        for LockUser in UserLockF.readlines():            #判断输入的用户是否已被锁定            if UserName == LockUser.strip("\n"):                sys.exit("用户已被锁定")        #遍历用户信息文件        for i in UserList.readlines():            #比对账户密码是否正确            if UserName == i.split(":")[0] and UserPass == i.split(":")[1].strip("\n"):                UserList.close()                UserLockF.close()                UserAcount = [0,UserName,i.split(':')[2],]                return UserAcount        else:            print("账号或密码错误!")            #密码不正确判断本次输入和上次输入用户名是否一致            if UserName == OldName:                RetryNum += 1            else:                RetryNum = 1                OldName = UserName    else:        #超过三次锁定用户        f = open("LockUserFile","a")        f.write(UserName+"\n")        f.close()        print("尝试密码过多,已被锁定!")#初始化Login函数,并接受返回值,包括用户登录状态、用户名和账户余额UserAcount = Login()#print(UserAcount)#创建空购物车列表SelectShopList = []#格式化已购商品总价SelectShopTotal = 0#如果Login函数执行成功接收的第一个返回值为0,开始购物车程序while UserAcount[0] == 0:    print(ChoiceOne) #打印商品列表    SelectFirst = input("请输入你的选择: ").strip() #选择商品-第一次选择,并去除空格    if int(SelectFirst) not in range(1,4): #判断选择是否在可选范围内        print("输入错误,请重新输入。")   #        continue                           #输入错误重新的输入    if int(SelectFirst) == 1:        print("你的余额是:",UserAcount[2])  #返回余额        continue                             #再次执行上面的循环    if int(SelectFirst) == 2:                 #选择的是进入商城        while True:            print(ChoiceTwo)                   #打印商城的功能列表            SelectSecond = input("请输入你的选择: ") #输入选择的商城功能            if int(SelectSecond) not in range(1,5):    #判断选择是否在可选列表                print("输入错误,请重新输入。")                continue                                #输入错误返回重输            if int(SelectSecond) == 1:                  #选择商品界面                while True:                             #进入商城购物程序                    for SL in ShopList:                        print(ShopList.index(SL),SL[0],SL[1],) #打印所有商品                    ShopSelect = input("请输入你要买的商品:").strip()  #选择需要的商品                    if int(ShopSelect) not in range(0,len(ShopList)):    #判断选择的是否在可选范围                        print("输入错误,请重新输入。")                        continue                                         #输入错误返回重新输入                    elif int(ShopSelect) == len(ShopList)-1:             #输入最后一个序列,返回上一级                        break                    else:                        SelectShopList.append(ShopList[int(ShopSelect)][0]) #加入选择的商品到选择商品的列表中                        SelectShopTotal += ShopList[int(ShopSelect)][1]     #计算商品总额                        print("你选择的商品有:")                          #打印所有选择的商品和价格                        for i in SelectShopList:                            print(i)                        print("总价为: ",SelectShopTotal)            if int(SelectSecond) == 2:                                      #查看已经选择的商品                if len(SelectShopList) == 0:                                #如果列表长度为一即没有选择任何商品                    print("你没有选择任何商品。")                else:                    for i in SelectShopList:                                #打印所有的商品                        print(i)                    print("总价为: ", SelectShopTotal)            if int(SelectSecond) == 3:                                         #结算                if int(UserAcount[2]) >= SelectShopTotal:                       #如果余额大于所选商品的金额                    UserBanlance = int(UserAcount[2]) - SelectShopTotal            #计算剩余的余额                    #打印所有购买的商品、总额、余额                    print("你购买的商品有:%s,消费总额是:%s,你的账户余额是%s"%(SelectShopList,SelectShopTotal,UserBanlance))                    for line in fileinput.input("UserInfo",inplace=1):                        line = line.replace(UserAcount[2],str(UserBanlance),)                        print(line,)                else:                    print("你的钱不够了,请重新购买。")  #余额不足以购买选择的商品                    SelectShopList.clear()                  #清空购物车                    continue            if int(SelectSecond) == 4:                      #退出                sys.exit("欢迎下次光临")    if int(SelectFirst) == 3:                               #退出        sys.exit("欢迎下次光临。")

代码示例

3、代码文件:

  1)LockUserFile      #用于锁定用户的文件

  2)README        #使用帮助

  3)UserInfo        #用户账号密码以及余额文件

      test:test:1230

      dukuan:dukuan:389352

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289