gaofeng0210 发表于 2018-8-10 08:15:10

python购物车功能实现

name = "gaowang"  
pwd = "123.abc"
  
list_he=[]          #定义空列表,后面接收
  
for i in range(3):
  
    username = input("请输入您的账号:")
  
    password = input("请输入您的密码:")
  
    if username == name and password == pwd: #判断帐号与密码是否符合
  
      print("恭喜,%s,登陆成功,欢迎您" % username)
  
      offer = int(input("请输入您的储值卡金额:"))
  
      print('''
  
      ------------------------------------
  
      --      欢迎光临购物商城         --
  
      ------------------------------------
  
      ''')      #格式化输出
  
      print('''
  
      1:获取商品列表
  
      2:充值购物卡
  
      3:退出系统
  
      ''')          #格式化输出
  
      gong_neng = int(input("请选择你的功能"))
  
      if gong_neng == 1:# 进入选购商品环节
  
            while True:
  
                shipin2 = [['牛奶', 20], ['肉干', 30], ['大米', 15], ['面包', 15], ['啤酒', 3.5]]
  
                for i, a in enumerate(shipin2, 1):   #循环打印商品列表
  
                  print("序号:%s" % i, "商品:%s" % a, "价格:%s元" % a)
  
                huo_qu = int(input("请输入你要购买的商品,输入退出"))
  
                if huo_qu > 0 and huo_qu <= len(shipin2):   #验证输入是否正确
  
                        j=shipin2 #购买的商品和价格
  
                        if j>offer :         #判断想要购买的商品价格是否超过了余额
  
                            print(&quot;您的余额不足,请及时充值&quot;)
  
                        else:
  
                            offer = offer - j    #算出购买商品后的价格
  
                            print(&quot;您购买的商品为%s&quot; % j, &quot;剩余金额为%s&quot; % offer)    #输出购买的商品
  
                            list_he.append(j)   #把已购买商品添加至集合中
  
                            print(&quot;您已经购买了%s&quot;%list_he)       #已购买商品集合
  
                elif huo_qu==0:
  
                  print(&quot;退出程序,再见&quot;)
  
                  for m in list_he :
  
                        print(&quot;您购买了%s&quot;%m)
  
                  break
  
                else:
  
                  print(&quot;商城货物暂时短缺,请输入正确的商品序号&quot;)
  
      elif gong_neng == 2:# 充值购物卡功能
  
            chongzhi=int(input(&quot;请输入您要充值的金额&quot;))
  
            if chongzhi>0:
  
                offer+=chongzhi
  
                print(&quot;充值成功,现余额为%s&quot;%offer)
  
            else:
  
                print(&quot;充值失败&quot;)
  
                break
  
      elif gong_neng==3:# 退出系统
  
            print(&quot;退出系统成功&quot;)
  
      break
  
    else:
  
      print(&quot;账号或密码输入不正确,请重新输入&quot;)
  

  
else:
  
    print(&quot;尝试用户名或密码超过最大次数,请重新运行程序&quot;)
页: [1]
查看完整版本: python购物车功能实现