Python Day14 作业

import os
import time

#
# 1、写函数,,用户传入修改的文件名,与要修改的内容,执行函数,完成批了修改操作
# def revise(path, txt, r_txt):
# with open(path, 'r+')as f:
# res = f.read()
# c_res = res.replace(txt, r_txt)
# f.seek(0)
# print(c_res)
# f.write(c_res)
#
#
# path=input('输入路径')
# txt=input('输入更改内容')
# r_txt=input('输入更改后内容')
# revise(path, txt, r_txt)
# 2、写函数,计算传入字符串中【数字】、【字母】、【空格] 以及 【其他】的个数

# txt = input('输入字符串')
#
#
# def count(txt):
# n_n = 0
# s_n = 0
# z_n = 0
# a_n = 0
# for i in txt:
# if i.isdigit():
# n_n += 1
# elif i == ' ':
# s_n += 1
# elif i.isalpha():
# z_n += 1
# else:
# a_n += 1
# print(f'数字个数{n_n},其他个数{a_n},空格个数{s_n},字母个数{z_n} ')
#
#
# count(txt)

# 3、写函数,判断用户传入的对象(字符串、列表、元组)长度是否大于5。
#
# def decide(txt):
# if len(txt) > 5:
# print('大于5')
# else:
# print('小于5')
#
#
# txt = (1, 23, 4, 5, 6, 7)
# decide(txt)
# 4、写函数,检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
#
# def change(list):
# if len(list) > 2:
# c_list = [x for x in list if list.index(x) < 2]
# else:
# print('长度小于2')
# return c_list
#
#
# list = [1, 23, 45, 5]
# c_list = change(list)
# print(c_list)

# 5、写函数,检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。
#
# def func(content):
# n_content = [x for x in content if content.index(x) % 2 != 0]
# return n_content
#
#
# print(func(content=[1,23,4,5,6]))
# 6、写函数,检查字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者。
dic = {"k1": "v1v1", "k2": [11, 22, 33, 44]}


# PS:字典中的value只能是字符串或列表
# def func(dic):
# for i in dic:
# value = dic[i]
# if len(value) > 2 and type(value) == list:
# n_value = [x for x in value if value.index(x) < 2]
# dic[i] = n_value
# if len(value) > 2 and type(value) == str:
# n_value=value[0:2]
# dic[i] = n_value
#
# return dic
#
#
# print(func(dic))
# 选做作业:同昨天
# def login():
# print('开始登录')
# login_t = False
# tag = False
# count = 0
# while count < 3:
# in_name = input('输入名字:')
# in_password = input('请输入密码:')
# with open('db.txt', encoding='utf-8')as f, open('.db.txt.swap', mode='w', )as f1:
# for i in f:
# name, password, money, lock = i.strip('\n').split(':')
# if in_name == name:
# tag = True
# print('用户存在,', end='')
# if password == in_password and lock == 'lock_on':
# print('账号被锁定,无法登陆')
# count = 3
# elif password == in_password and lock != 'lock_on':
# print('登录成功')
# count = 3
# login_t = True
# atm_name = in_name
# else:
# print('密码错误,三次后将被锁定')
# count += 1
# if count == 3:
# print('三次错误被锁定')
# lock = 'lock_on'
# i = f'{name}:{password}:{money}:{lock}\n'
# f1.write(i)
# else:
# if not tag:
# print('用户不存在')
# os.remove('db.txt')
# os.rename('.db.txt.swap', 'db.txt')
# return login_t, atm_name
#
#
# # 4、编写注册功能
# def register():
# print('注册')
# in_name = input('输入名字:')
# in_password = input('请输入密码:')
# with open('db.txt', mode='a', encoding='utf-8') as f1, open(r'db.txt', encoding='utf-8')as f2:
# for i in f2:
# a = i.strip('\n').split(':')
# if in_name == a[0]:
# print('已注册')
# break
# else:
# f1.write(f'{in_name}:{in_password}:0:lock_off\n')
# print('注册成功')
#
#
# def out():
# global tag
# tag = False
#
#
# def atm(user):
# atm_f = {
# 1: take_money,
# 2: invest_money,
# 3: remove_money,
# 4: show_money
# }
# info = """
# 1 取钱
# 2 充钱
# 3 转钱
# 4 查询余额
# """
# print(info)
# chose = input('输入选择:')
# if int(chose) in atm_f:
# atm_f.get(int(chose))(user)
# else:
# print('输入不正确的选择')
#
#
# def change_dbm(user, c_money):
# with open('db.txt', mode='r', encoding='utf-8')as f, \
# open('.db.txt.swap', mode='w')as f2:
# for i in f:
# a = i.strip('\n').split(':')
# name, password, money, lock = a
# if user == name:
# money = int(money)
# money = money + int(c_money)
# i = f'{name}:{password}:{money}:{lock}\n'
# f2.write(i)
# os.remove('db.txt')
# os.rename('.db.txt.swap', 'db.txt')
# return money
#
#
# def take_money(user):
# c_money = input('输入要取的钱')
# money = -int(c_money)
# change_dbm(user, money)
#
#
# def invest_money(user):
# c_money = input('输入要存入的钱')
# change_dbm(user, c_money)
#
#
# def remove_money(user):
# pass
#
#
# def show_money(user):
# print(change_dbm(user))
#
#
# # 5、编写用户认证功能
# res = {
# out: 0,
# login: 1,
# register: 2
# }
# tag = True
# while tag:
# msg = """
# 0 退出
# 1 登录
# 2 注册
# """
# print(msg)
# cmd = input('请输入命令编号>>: ').strip()
# for i in res:
# if str(res.get(i)) == cmd:
# info = i()
# if_login, user = info
# if if_login:
# atm(user)

# 选做题:编写ATM程序实现下述功能,数据来源于文件db.txt
# 1、充值功能:用户输入充值钱数,db.txt中该账号钱数完成修改
# 2、转账功能:用户A向用户B转账1000元,db.txt中完成用户A账号减钱,用户B账号加钱
# 3、提现功能:用户输入提现金额,db.txt中该账号钱数减少
# 4、查询余额功能:输入账号查询余额

# 选做题中的选做题:登录功能
# 用户登录成功后,内存中记录下该状态,上述功能以当前登录状态为准,必须先登录才能操作
#

猜你喜欢

转载自www.cnblogs.com/AaronY/p/12518895.html