python 容器 用户注册登录系统

发布时间:2019-03-04 15:58:43编辑:auto阅读(1933)

    1. 列表和普通变量有什么区别
      列表是数据类型,普通变量是用来存储数据的
      可以把列表赋值给普通变量

    2.存在列表 a = [11, 22, 33], 如何向列表中添加(增)新元素 44
      a.append(44)
      或者
      a.insert(3,44) #索引号为3

    3.对列表排序
      a = [11,22,33,2]
      b = sorted(a) #创建了一个新的列表 ,a.sort()修改a列表
      print(b) # [2, 11, 22, 33, 44]

      b = a.sort()
      print(b) # None

      print(a) # [2, 11, 22, 33, 44]

    4.存在字典 info = {'name':'李四'}, 删除元素 name
      del info["name"]
      或者
      info.pop("name")

    5.字典和列表的不同以及使用字典的目的
      字典是以键值对的形式存储数据的,字典是无序的,通过键获取到对应值
      列表是有序的,通过下标获取到值
      使用字典可以存储一些有标识性的数据,可以为数据起一个特定的名字。

    列表增加元素方式: list.append(s),list.extend(list2),list.insert(index,s),

    1 a = [11,22,33,2]
    2 b= [22,44]
    3 str = '2'
    4 a.extend(str)  # [11, 22, 33, 2, '2']
    5 print(a)
    6 a.extend(b)  # [11, 22, 33, 2, '2', 22, 44]
    7 print(a)
    View Code

     

    字典(hash存储):dict[key] = value,dict.update(dict2) ,

    set(hash存储):set.add(s),set.update(iterable)

    1 set1 = {10,3.13,20,"hello"}
    2 set2 = {10,18}
    3 set1.update(set2)  # 10未被添加,18被加入
    4 print(set1)
    5 
    6 list = [1,[2,3]]
    7 set1.update(list)  # TypeError: unhashable type: 'list'
    View Code

    列表删除元素的方式,del list[index],list.pop(index),list.remove(s)

    字典的删除方式,del dict[key], dict.pop(key)

    set的删除方式,set.pop(),set.remove(s)

    
    
    1 set1.pop()  # 删除最后一个,但最后一个是随机的,所以可认为随机删除
    2 print(set1)
    3 
    4 set1.remove("15")
    5 print(set1)
    View Code

     

    用户注册登录系统

     1 user_dict = {}
     2 user_list = []
     3 log = True
     4 
     5 def prelog():
     6     active = True
     7     while active:
     8         user_name = input("请输入你的昵称")
     9         len_username = len(user_name)
    10         if len_username > 6 and len_username < 20:
    11             while active:
    12                 user_pass = input("请输入你的密码")
    13                 len_userpass = len(user_pass)
    14                 if len_userpass > 8 and len_userpass < 20:
    15                     while active:
    16                         user_age = input("请输入你的年龄")
    17                         if  user_age.isdigit():
    18                             user_dict['昵称'] = user_name
    19                             user_dict['密码'] = user_pass
    20                             user_dict['年龄'] = user_age
    21                             user_list.append(user_dict)
    22                             active = False
    23                         else:
    24                             print("请输入纯数字")
    25                             continue
    26                 else:
    27                     print("密码长度不合法")
    28                     continue
    29         else:
    30             print("昵称长度不合法")
    31             continue
    32 
    33 def login():
    34     signal2 = True
    35     while True:
    36         if signal2:
    37             global log
    38             log = False
    39             signal = False
    40             user_name = input("请输入你的用户名")
    41             user_pass = input("请输入你的密码")
    42             for e in user_list:
    43                 if e.get("昵称") == user_name:
    44                     real_pass = e["密码"]
    45                     if real_pass == user_pass:
    46                         signal = True
    47                         print("登录成功")
    48                         print("运行成功")
    49                         ask = input("是否退出?y/n")
    50                         if ask == 'y':
    51                             signal2 = False
    52                             log = True
    53                             break  # 直接退出for循环
    54                         else:
    55                             signal2 = False
    56                             break  #直接退出for循环
    57             if not signal:
    58                 print("用户名或者密码错误")
    59                 continue
    60         else:
    61             break  # 直接退出while循环
    62 
    63 while True:
    64     choice = input("""1.注册
    65 2.登录
    66 3.注销
    67 4.退出
    68     """)
    69     if choice == '1':
    70         prelog()
    71     elif choice == '2':
    72         if log == True:
    73             login()
    74             # continue
    75         else:
    76             print("用户接口被占用")
    77     elif choice == '3':
    78         if log == True:
    79             print("无用户登录")
    80         else:
    81             log = True
    82     elif choice == '4':
    83         if log == True:
    84             break
    85         else:
    86             print("请先注销")
    87     else:
    88         print("请输入1-4")
    View Code

     

关键字