初识python-day2

作业

'''
1.让用户输入用户名与密码 2.校验用户名是否存在 3.用户名存在后检验密码是否正确,若正确打印“登陆成功” 否则打印“用户名或密码错误”,并让用户重新输入 4.用户密码输入错误超过三次则退出循环。 ''' def login(): with open('user.txt', 'r', encoding='utf-8') as f: user_info = f.read().split(',') count = 1 while i <= 3: user = input('请输入用户名:').strip() pwd = input('请输入密码:').strip() for index in range(len(user_info)): if user_info[index] == user: if user_info[index+1] == pwd: print("登录成功!") count = 4 break else: print("用户名或密码错误!") count = count+1 break login()

 课堂总结

一、列表操作(续)

1.insert()  # 插入
# # 第一个参数: 索引 第二个参数: 插入的值
# list1 = ['tank', 18, 'male', 3.0, 9, '广东', 'tank', [1, 2]]
#
# list1.insert(2, 'oldboy')
#
# print(list1)
#
2.pop() # 取出
3.remove() # 移除
#
4.count() # 查看某个值的个数
# print(list1.count('tank'))
#
5.index() # 查看值的索引
# print(list1.index('广东'), '---广东')
#
6.clear() # 清空列表的值
# list1.clear()
# print(list1)
#
#
7.copy() # 浅拷贝
# # 将list1的内存地址浅拷贝赋值给list2
# list2 = list1.copy()
# print(list2, '添加值前')
#
# # 将list1的原地址直接赋值给了list3
# list3 = list1
# print(list3, '添加值前')
#
# # 深拷贝()
# from copy import deepcopy
# # 将list1的值深拷贝赋值给list4
# list4 = deepcopy(list1)
#
# # 追加jason到list1中国
# list1.append('jason')
#
# print(list2, '添加值后')
# print(list3, '添加值后')
#
# # 给list1中的可变列表进行追加值
# list1[8].append('tank')
#
# # 打印直接赋值、深、浅拷贝的结果
# # 浅拷贝: list1的列表中外层值改变对其不影响
# # 但对list1中的可变类型进行修改则会随之改变值
# print(list2)
# print(list3)
#
# # 深拷贝: 把list1中的所有值完全拷贝到一个新的地址中
# # 进而与list1完全隔离开
# print(list4)
#
#
8.extend() # 合并
# list1 = [1, 2, 3]
# list2 = [4, 5, 6]
# list1.extend(list2)
# print(list1)
#
9.reverse() # 反转
# list1.reverse()
# print(list1)
#
10.sort() # 排序
# list3 = [1, 3, 5, 8, 10, 2, 4, 6]
# # 升序
# # list3.sort()
# # print(list3)
#
# # 降序
# list3.sort(reverse=True)
# print(list3)

注:
# tab : 往右空四个空格
# shift + tab : 往左减四个空格

二、字典操作
# 字典的内置方法(字典是无序的)
1、按照key取/存值
# dict1 = {'name': 'liwei', 'age': 18, 'sex': 'male', 'school': '安徽工程大学'}
#
# # 根据key取liwei的学校
# print(dict1['school'])
# print(dict1['sal'])
#
# # get()
# # 第一个参数是字典的key
# # 第二个参数是默认值,若key存在则取key对应的值,否则取默认值
# print(dict1.get('school', '华南理工大学'))
# print(dict1.get('sal', '15000'))
#
2、len
# print(len(dict1)) # 4
#
3、成员运算in和not in
# print('name' in dict1) # True
# print('sal' in dict1) # False
# print('sal' not in dict1) # True
#
4、删除
# del dict1["name"]
# print(dict1)
#
# # pop()
# # 根据字典中的key取出对应的值赋值给变量name
# name = dict1.pop('name')
# print(dict1)
# print(name)
#
# # 随机取出字典中的某个值
# dict1.popitem()
# print(dict1)
#
5、keys、values、items
# print(dict1.keys())
# print(dict1.values())
# print(dict1.items())
#
6、循环
# # 循环字典中所有的key
# for key in dict1:
# print(key)
#
7、update()
# print(dict1)
# dict2 = {"work": "student"}
# # 把dict2加到dict1字典中
# dict1.update(dict2)
# print(dict1)

三、 元组类型

#(在小括号内,以逗号隔开存放多个值)
# 注意: 元组与列表的区别,元组是不可变类型,列表是可变类型。

# tuple1 = (1, 2, 3, 4, 5, 6)
# print(tuple1)
优先掌握
1.按索引取值
# print(tuple1[2])
#
2.切片(顾头不顾尾)
# print(tuple1[0:6]) # (1, 2, 3, 4, 5, 6)
#
# # 步长
# print(tuple1[0:6:2]) # (1, 3, 5)
#
3.长度
# print(len(tuple1)) # 6
#
4.成员运算 in 和 not in
# print(1 in tuple1) # True
# print(1 not in tuple1) # False
#
5.循环
# for line in tuple1:
# print(line)
四、集合类型(一般用于去重)
# 在{}以逗号隔开,可存放多个值,但集合会自带默认去重功能。
# set1 = {1, 2, 3, 4, 2, 1, 3, 4}
# print(set1)
#
# # 集合是无需的
# set1 = set()
# set2 = {}
# print(set1)
# print(set2)
#
# set2['name'] = 'tank'
# print(type(set2))


猜你喜欢

转载自www.cnblogs.com/lweiser/p/11013556.html