第2周需求


"""
1.根据用户输入内容打印其权限
'''
jason --> 超级管理员
tom --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
'''
# 1.获取用户名
username = input('username>>>:').strip()
# 2.判断用户身份
if username == 'jason':
print('超级管理员')
elif username == 'tom':
print('普通管理员')
# 3.有两种方式
# elif username == 'jack' or username == 'rain':
elif username in ['jack', 'rain']:
print('业务主管')
else:
print('普通用户')
2.编写用户登录程序
要求:有用户黑名单 如果用户名在黑名单内 则拒绝登录
eg:black_user_list = ['jason','kevin','tony']
如果用户名是黑名单以外的用户则允许登录(判断用户名和密码>>>:自定义)
eg: oscar 123
black_user_list = ['jason', 'kevin', 'tony']
# 1.获取用户名
username = input("username>>>:").strip()
# 2.判断是否是黑名单用户
if username in black_user_list:
print('不好意思 您已经被拉黑了')
else:
password = input('password>>>:').strip()
if username == 'oscar' and password == '123':
print('登录成功')
else:
print('用户名或密码错误')
3.编写一个用户登录程序
用户如果登录失败 则只能尝试三次
用户如果登录成功 则直接结束程序
# 提前定义一个记录用户尝试次数的计数器
count = 1
while count < 4:
username = input('username>>>:').strip()
password = input('password>>>:').strip()
if username == 'jason' and password == '123':
print('登录成功')
break
else:
print('用户名或密码错误')
count += 1
4.编写一个用户登录程序
用户如果登录失败 可以无限制尝试
用户如果登录成功 则进入内层循环 获取用户输入的指令 并打印该指令
eg: 用户输入cmd指令 则程序打印'正在执行您的指令:cmd'
给用户提供一个退出命令 用户一旦输入则退出整个程序
eg: 用户输入q指令 则程序先打印该指令再结束
flag = True
while flag:
username = input('username>>>:').strip()
password = input('password>>>:').strip()
if username == 'jason' and password == '123':
print('登录成功')
while flag:
cmd = input('请输入您的指令>>>:').strip()
if cmd == 'q':
flag = False
print(f'正在执行您的命令:{cmd}')
else:
print('用户名或密码错误')
5.编写猜年龄的游戏
代码提前定义出真实的年龄 之后获取用户猜测的年龄判断
用户有三次猜测的机会 如果用完则提示用户是否继续尝试
用户输入y则继续给三次机会 如果用户输入q则直接结束程序
real_age = 18
count = 1
while True:
# 判断当前用户是第几次猜测
if count == 4:
is_guess = input('您已经猜测了三次 是否继续(y/n)>>>:').strip()
if is_guess == 'y':
count = 1 # 让计数器重新置为1
else:
print('欢迎下次继续猜')
break
guess_age = input('请输入猜测的年龄>>>:').strip()
if not guess_age.isdigit():
print('年龄必须是纯数字')
continue
guess_age = int(guess_age)
if guess_age > real_age:
print('猜大了')
count += 1
elif guess_age < real_age:
print('猜小了')
count += 1
else:
print('恭喜你猜对了!!!')
break
6.计算1-100所有的数之和
count = 0
for i in range(1, 101):
count += i
print(count)
7.编写代码打印出下列图形(ps:for循环嵌套)
*****
*****
*****
*****
for i in range(4): # 外层循环一次
for j in range(5): # 内层循环五次
print('*', end='') # 每次打印不换行
print() # 自带换行
8.基于字符串充当数据库完成用户登录(基础练习)
data_source = 'jason|123' # 一个用户数据
获取用户用户名和密码 将上述数据拆分校验用户信息是否正确
data_source = 'jason|123'
username = input('username>>>:').strip()
password = input('password>>>:').strip()
# real_name, real_pwd = ['jason', '123']
real_name, real_pwd = data_source.split('|') # 切割 解压赋值
if username == real_name and password == real_pwd:
print('登录成功')
else:
print('用户名或密码错误')
9.基于列表充当数据库完成用户登录(拔高练习) # 多个用户数据
data_source = ['jason|123', 'kevin|321','oscar|222']
data_source = ['jason|123', 'kevin|321', 'oscar|222']
username = input('username>>>:').strip()
password = input('password>>>:').strip()
for data in data_source: # 'jason|123' 'kevin|321'
real_name, real_pwd = data.split('|')
if real_name == username and real_pwd == password:
print('登录成功')
break
else:
print('用户名或密码错误')
10.2.编写员工管理系统
1.添加员工信息
2.修改员工薪资
3.查看指定员工
4.查看所有员工
5.删除员工数据
提示:用户数据有编号、姓名、年龄、岗位、薪资
数据格式采用字典:思考如何精准定位具体数据>>>:用户编号的作用

11.去重下列列表并保留数据值原来的顺序
eg: [1,2,3,2,1] 去重之后 [1,2,3]
l1 = [2,3,2,1,2,3,2,3,4,3,4,3,2,3,5,6,5]
l1 = [2, 3, 2, 1, 2, 3, 2, 3, 4, 3, 4, 3, 2, 3, 5, 6, 5]
new_list = []
for i in l1:
if i not in new_list:
new_list.append(i)
print(new_list)
12.统计列表中每个数据值出现的次数并组织成字典展示
eg: l1 = ['jason','jason','kevin','oscar']
结果:{'jason':2,'kevin':1,'oscar':1}
真实数据
l1 = ['jason','jason','kevin','oscar','kevin','tony','kevin']
l1 = ['jason', 'jason', 'kevin', 'oscar', 'kevin', 'tony', 'kevin']
new_dict = {}
for i in l1: # 'jason' 'jason' 'kevin'
if i not in new_dict:
new_dict[i] = 1 # {'jason':1}
else:
new_dict[i] += 1 # {'jason':1+1}

print(new_dict) # {'jason': 2, 'kevin': 3, 'oscar': 1, 'tony': 1}
13.编写简易版本的拷贝工具
自己输入想要拷贝的数据路径 自己输入拷贝到哪个地方的目标路径
任何类型数据皆可拷贝
ps:个别电脑C盘文件由于权限问题可能无法拷贝 换其他盘尝试即可
source_path = input('请输入想要拷贝的文件路径>>>:').strip()
target_path = input('请输入目的地址路径>>>:').strip()
with open(source_path,'rb') as read_f,open(target_path,'wb') as write_f:
for line in read_f:
write_f.write(line)
14.利用文件充当数据库编写用户登录、注册功能
文件名称:userinfo.txt
基础要求:
用户注册功能>>>:文件内添加用户数据(用户名、密码等)
用户登录功能>>>:读取文件内用户数据做校验
ps:上述功能只需要实现一次就算过关(单用户) 文件内始终就一个用户信息
while True:
print("""
# 1.
# 注册功能
# 2.
# 登录功能
""")
func_id = input('请输入想要执行的功能编号>>>:').strip()
if func_id == '1':
username = input('username>>>:').strip()
password = input('password>>>:').strip()
user_data = f'{username}|{password}'
with open(r'userinfo.txt','w',encoding='utf8') as f:
f.write(user_data)
print(f'{username}注册成功')
elif func_id == '2':
username = input('username>>>:').strip()
password = input('password>>>:').strip()
with open(r'userinfo.txt','r',encoding='utf8') as f:
file_data = f.read() # 'jason|123'
real_name, real_pwd = file_data.split('|')
if username == real_name and password == real_pwd:
print('登录成功')
break
else:
print('用户名或密码错误')
拔高要求:
用户可以连续注册
用户可以多账号切换登录(多用户) 文件内有多个用户信息
ps:思考多用户数据情况下如何组织文件内数据结构较为简单
提示:本质其实就是昨天作业的第二道题 只不过数据库由数据类型变成文件
"""
while True:
print("""
1.注册功能
2.登录功能
""")
func_id = input('请输入想要执行的功能编号>>>:').strip()
if func_id == '1':
username = input('username>>>:').strip()
# 先判断当前用户名是否已存在
with open(r'userinfo.txt', 'r', encoding='utf8') as f:
for line in f:
if username == line.split('|')[0]:
print('用户名已存在')
break
else:
password = input('password>>>:').strip()
user_data = f'{username}|{password}\n' # 一个用户一行 所以需要换行符
with open(r'userinfo.txt', 'a', encoding='utf8') as f:
f.write(user_data)
print(f'{username}注册成功')
elif func_id == '2':
username = input('username>>>:').strip()
password = input('password>>>:').strip()
with open(r'userinfo.txt', 'r', encoding='utf8') as f:
for line in f:
real_name, real_pwd = line.split('|') # ['jason', '123\n']
if real_name == username and password == real_pwd.strip('\n'):
print('登录成功')
break
else:
print('用户名或密码错误')
posted @ 2022-08-27 17:25  呼长喜  阅读(13)  评论(0编辑  收藏  举报