今日作业:
1、函数对象优化多分支if的代码练熟
def log_in():
print('登录功能')
def register():
print('注册功能')
def check_balance():
print('查询余额')
def withdraw():
print('提现功能')
def transfer():
print('转账功能')
dic = {
'0':{'退出',exit},
'1':{'注册',register},
'2':{'登录',log_in},
'3':{'查询余额',check_balance},
'4':{'提现',withdraw},
'5':{'转账',transfer}
}
while True:
for a in dic:
print(a,dic[a][0])
cmd = input('请输入命令编号:').strip()
if not cmd.isdigit():
print('选择命令选项')
continue
if cmd == '0':
break
if cmd in dic:
dic[cmd][1](name)
else:
print('命令无效')
2、编写计数器功能,要求调用一次在原有的基础上加一
def num():
x = 0
def counter():
nonlocal x
x+=1
return x
return counter
couter = num()
print(couter())
print(couter())
print(couter())
print(couter())
print(couter())
周末作业
编写ATM程序实现下述功能,数据来源于文件db.txt
0、注册功能:用户输入账号名、密码、金额,按照固定的格式存入文件db.txt
1、登录功能:用户名不存在,要求必须先注册,用户名存在&输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
下述操作,要求登录后才能操作
1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
4、查询余额功能:输入账号查询余额
获取所有用户的数据
def get_all_users():
with open('db.txt', 'r', encoding='utf-8') as f:
for line in f:
user, pwd, balance = line.strip().split('😂
# tank:9527:100000 ---> all_user_dict ---> {'tank': ["9527", 100000]}
all_user_dict[user] = [pwd, balance]
调用函数执行函数体代码
get_all_users()
注册功能
def register():
# 打开文件,获取文件中所有用户的数据
# with open('db.txt', 'r', encoding='utf-8') as f:
# for line in f:
# user, pwd, balance = line.strip().split(':')
# # tank:9527:100000 ---> all_user_dict ---> {'tank': ["9527", 100000]}
# all_user_dict[user] = [pwd, balance]
# 文件关闭后,相当于字典中有了所有的用户
'''
all_user_dict
{
'tank': ['9527','100000']
'egon': ['321','250']
'alex': ['567','100']
}
'''
while True:
# 让用户输入用户名 ---> tank
username = input('请输入用户名: ').strip()
# 1、校验用户是否存在 in
# if tank in ['tank', 'egon', 'alex']:
if username not in all_user_dict:
# 2、若用户不存在,则继续让用户输入密码,进行注册
password = input('请输入密码: ').strip()
re_password = input('请输入密码: ').strip()
# 3、判断两次密码是否一致
if password == re_password:
# 4、可以让用户输入注册的金额
balance = input('请输入注册金额:').strip()
if balance.isdigit():
# 5、将新的用户数据追加到db.txt文件中
with open('db.txt', 'a', encoding='utf-8') as f:
f.write(f'{username}:{password}:{balance}')
print(f'[{username}]注册成功')
break
else:
print('请输入数字类型')
else:
print('注册失败!')
else:
# 若存在,则让用户重新输入
print('当前用户已存在,请重新输入!')
测试功能
register()
# 1、登录功能:用户名不存在,要求必须先注册,用户名存在&
输错三次锁定,登录成功后记录下登录状态(提示:可以使用全局变量来记录)
全局变量
login_user = None
用于记录用户是否锁定,
locked_users = {}
登录功能
def login():
# with open('db.txt', 'r', encoding='utf-8') as f:
# for line in f:
# user, pwd, balance = line.strip().split('😂
# # tank:9527:100000 ---> all_user_dict ---> {'tank': ["9527", 100000]}
# all_user_dict[user] = [pwd, balance]
while True:
# 1、请输入用户名,判断用户名是否存在
username = input('请输入用户名: ').strip()
# 若用户不存在
if username not in all_user_dict:
# 不存在,则调用注册函数
register()
continue
# 判断该用户是否被锁定,若锁定则让其退出登录
if all_user_dict.get(username)[2]: # True
print('当前用户已被锁定!')
break
count = 0
while count < 3:
# 2、若用户存在,则继续输入密码,进行登录校验
password = input('请输入密码: ').strip()
# all_user_dict.get(username) --> [pwd, balance]
# passsword == pwd
if password == all_user_dict.get(username)[0]:
# 3、若用户登录成功后,则引用外部传入的全局变量进行修改,
# 将当前登录的用户名存放在全局变量login_user中,用于证明已经有用户登录了
global login_user # 默认为 None
login_user = username
print('登录成功!')
break
else:
count += 1
print('密码错误!')
# 若count == 3时,证明用户输错三次了,则将该用户锁定
if count == 3:
# {tank: True}
# locked_users[username] = True
# {'tank': ["9527", 100000]}
# ["9527", 100000] -----> ["9527", 100000, True]
all_user_dict[username][2] = True
break