santaclaus 发表于 2018-8-10 06:38:48

重构优化Python购物车

# -*- coding:utf-8 -*-  

  
def regiter():
  
    while True:
  
      username = input("请输入您的账号:").strip()
  
      password = input("请输入您的密码:").strip()#去除空格及换号符
  
      with open('register',encoding='UTF-8')as f1:
  
            for line in f1:#循环读取注册文件中的内容
  
                line_list=line.strip().replace(',',',').split(',')#去除空格及换号符以及把中文逗号换成英文逗号
  
                #print(type(line_list))
  
                if username==line_list:
  
                  print("用户名已经存在,请重新输入")
  
                  break
  
            else:
  
                with open('register', encoding='UTF-8',mode='a')as f2:
  
                  f2.write('\n{},{}'.format(username,password))
  
                  print("恭喜你注册成功%s"%username)
  
                  return True #结束函数
  

  
def login():
  
    i=0#计数器
  
    while i<3:#超过3次后,登陆失败
  
      username = input(&quot;请输入您的账号:&quot;).strip()
  
      password = input(&quot;请输入您的密码:&quot;).strip()# 去除空格及换号符
  
      with open('register',encoding='UTF-8')as f1:
  
            for line in f1:#循环读取注册文件中的内容
  
                line_list=line.strip().replace(',',',').split(',')#去除空格及换号符以及把中文逗号换成英文逗号
  
                #print(type(line_list))
  
                if username==line_listand password==line_list:
  
                  print(&quot;*******登陆成功*******&quot;)
  
                  return True
  
            else:
  
                print(&quot;账户或密码输入错误&quot;)
  
                i+=1
  
def shopping():
  
    list_he=[]
  
    offer=input(&quot;请输入您的储值卡金额:&quot;).strip()
  
    if offer.isdigit():
  
      offer=int(offer)
  
      while True:
  
            shipin2 = [['牛奶', 20], ['肉干', 30], ['大米', 15], ['面包', 15], ['啤酒', 3.5]]
  
            for i, a in enumerate(shipin2, 1):# 循环打印商品列表
  
                print(&quot;序号:%s&quot; % i, &quot;商品:%s&quot; % a, &quot;价格:%s元&quot; % a)
  
            huo_qu = input(&quot;请输入你要购买的商品,输入退出&quot;).strip()
  
            if huo_qu.isdigit():
  
                huo_qu=int(huo_qu)
  
                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)# 已购买商品集合
  
                        continue
  
                elif huo_qu == 0:
  
                  print(&quot;退出程序,再见&quot;)
  
                  for m in list_he:
  
                        print(&quot;您购买了%s&quot; % m)
  
                  break
  
                else:
  
                  print(&quot;商城货物暂时短缺,请输入正确的商品序号&quot;)
  
    else:
  
      print(&quot;您输入的有非法字符,请重新输入&quot;)
  
choice_dict={
  
    1:regiter,
  
    2:login,
  
    3:shopping
  
}
  
while True:
  
    print('''-------欢迎光临购物商场-------
  
          1:注册
  
          2:登陆
  
          3:购物
  
          4:退出
  
          ''')
  
    choice=input(&quot;请选择序号&quot;).strip()
  
    if choice.isdigit():
  
      choice=int(choice)
  
      if choice>0 and choice<=len(choice_dict):
  
            choice_dict()
  
      else:
  
            print(&quot;您输入的超出范围&quot;)
  
    else:
  
      print(&quot;您输入的有非法字符&quot;)
页: [1]
查看完整版本: 重构优化Python购物车