2019-06-12 python day02

今日作业

题目:

让用户输入用户名和密码

​ 校验用户名是否存在

​ 用户名存在后校验密码是否正确,若正确打印“登录成功”,

​ 否则打印“用户名或密码错误”,并让用户重新输入

​ 用户密码输入错误超过三次退出循环

hw.py

#模拟后台用户信息数据库

name_dict = {'John Wick':'2014','Ethan Hunt':'2000','Bourne':'2002'}

# 登录信息验证

def sign_in():

​ i = 0

​ while True:

​ name = input('请输入用户名:')

​ pwd = input('请输入密码:')

​ #校验用户名是否存在

​ if(name in name_dict.keys() and pwd == name_dict.get(name)):

​ print('登陆成功!')

​ break

​ else:

​ if(i < 2):

​ print('用户名或密码错误,请重新输入')

​ i = i + 1

​ elif(i == 2):

​ print('您已输错三次,请明天再试')

​ break

sign_in()

课程回顾

主要内容:

  1. 数据类型剩余的内置方法(list,dict,tuple,set)
  2. 字符编码
  3. 文件处理
  4. 函数基础

list方法

insert()插入

list1 = ['ktan',18,'male',3.0,9,'光之国']

list1.insert(2,'Young OG')

print(list1)

count()

list1.append('ktan')

print(list1.count('ktan'))

index()

print(list1.index('光之国'),'---光之国')

copy()

list1.append(['Q'])

print(list1)

  • 浅拷贝

list2 = list1.copy()

  • 深拷贝

from copy import deepcopy

list4 = deepcopy(list1)

list3 = list1

list1.append('RUChange')

print(list2,'Change?')

print(list3,'Change?')

print(list4,'Change?')

list1[list1.index(['Q'])].append('M')

print(list2,'copy after model list change')

print(list3,'= after model list change')

print(list4,'deepcopy after model list change')

clear()

list1.clear()

print(list1)

print(list2)

list3 = list1

print(list3)

list1.append('Real OG')

print(list1,list3)

extends

list1 = [1,2,3]

list2 = [4,5,6]

list1.extend(list2)

print(list1)

sort()

list3 = [1,2,4,7,32,833,55]

# default ascending

list3.sort()

print(list3)

# descending

list3.sort(reverse = True)

print(list3)

字典内置方法

1.get/set value by key

dict1 = {'name':'DeathGum','age':20,'school':'Unkown'}

# get()

print(dict1.get('school','xxx'))

2.length of dict

print(len(dict1))

3.成员运算

print('name' in dict1) # True

print('sal' in dict1) # False

print('sal' not in dict1) # True

4.delete

name = dict1.pop('name')

print(dict1)

print(name)

dict1.popitem() # 随机删除某个item

print(dict1)

5.keys values items

dict1 = {'name':'DeathGum','age':20,'school':'Unkown'}

print(dict1.keys())

print(dict1.values())

print(dict1.items())

6.loop

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))

文件操作

import requests

# # 文件读写基本操作

# # 对文本进行操作

# # open ('file name',mode = 'wt',encoding='utf-8')

# f = open('/Users/nadia/Code/Python/test.txt',mode='wt',encoding='utf-8')

# f.write('aifhjsdvlafsddkfj')

# f.close

# f = open('/Users/nadia/Code/Python/test.txt',mode='r',encoding='utf-8')

# print(f.read())

# # append mode

# f = open('/Users/nadia/Code/Python/test.txt',mode='a',encoding='utf-8')

# f.write('\nfghjk')

# f.close

# #结合网络爬虫进行图片读写

str1 = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1560334128444&di=2258333ac43a3451265b9ef0081e6455&imgtype=0&src=http%3A%2F%2Fimg.tw.mm52.com%2Ft%2Ftakeshi_kitano%2Ftakeshi_kitano_0080_128px.jpg'

r = requests.get(str1)

with open('nmb.jpg',mode='wb') as f:

​ f.write(r.content)

# 视频文件读写

# with open('tianyan_sys.mp4','rb')as f,open('tianyan_sys_copy.mp4','wb')as w:

# for line in f:

# f.read(line)

# w.write(line)

# f.close()

# w.close()

基本函数

def fib(n):

​ result = []

​ a,b = 0,1

​ while a

​ result.append(a)

​ a,b = b,a+b

​ return result

f = fib

print(f(100))

def register():

​ while True:

​ user = input('请输入用户名:').strip()

​ pwd = input('请输入密码:').strip()

​ re_pwd = input('请确认密码:').strip()

​ if pwd == re_pwd:

​ # 格式字符串的三种方式

​ '''

​ user_info = '用户名:%s , 密码:%s',user,pwd

​ user_info = '用户名:{}, 密码:{}'.formate(user,pwd)

​ '''

​ user_info = f'用户名:{user},密码:{pwd}'

​ with open(f'{user}.txt','w',encoding='utf-8')as f:

​ f.write(user_info)

​ break

​ else:

​ print('两次密码不一致,请重新输入')

#定义阶段和调用阶段未定义函数语句在interpreter中报错与不报错

register()

你可能感兴趣的:(2019-06-12 python day02)