需求:用户登录度小满家庭记账系统(管理员账户:admin/123456,普通用户:lisi/123456),若满足三次之内登录成功,进度进入家庭记账界面。若管理登录成功,可对用户的日常理财流程进行打印。
1.普通用户记账界面
2.管理员可管理界面
import sys
# 管理员账户
manager = ['admin', '123456']
# 普通用户账户
user = ['lisi', '123456']
# 系统数据
data = []
# ID
id = 0
# 标题
title = ['ID', '类别', '账户', '金额', '时间', '说明']
# 添加
def add():
global id
global data
id += 1
category = input('请输入要添加的账务类别:')
account = input('请输入所使用的账户:')
money = input('请输入支出金额:')
time = input('请输入账务日期:')
state = input('请输入账务说明:')
data.append(id)
data.append(category)
data.append(account)
data.append(money)
data.append(time)
data.append(state)
print('账务添加成功!')
# 编辑
def update():
global data
id = int(input('请输入ID:'))
judge = data.count(id)
if judge == 0:
print('您输入的ID有误!')
return
dataIndex = data.index(id)
category = input('编辑账务类别(输入N/n跳过):').lower()
if category != 'n':
data[dataIndex+1] = category
account = input('编辑账户(输入N/n跳过):').lower()
if account != 'n':
data[dataIndex+2] = account
money = input('编辑账务金额(输入N/n跳过):').lower()
if money != 'n':
data[dataIndex+3] = money
time = input('编辑账务时间(输入N/n跳过):').lower()
if time != 'n':
data[dataIndex+4] = time
state = input('编辑账务说明(输入N/n跳过):').lower()
if state != 'n':
data[dataIndex+5] = state
print('账务编辑更新成功!')
# 删除
def delete():
global data
id = int(input('请输入要删除的业务ID:'))
dataIndex = data.index(id)
if dataIndex == -1:
print('系统中没有这条数据!')
return
data[dataIndex:dataIndex+6] = []
print('删除账务成功!')
# 打印标题
def printTitle():
for name in title:
print('%-20s\t' % name, end="")
print('')
print('-' * 150)
# 查询
def query():
condition = input('1.查询所有 2.按条件查询\n')
print('-' * 150)
if condition == '1':
printTitle()
for i in range(0, len(data), 6):
subdata = data[i:i + 6]
for j in subdata:
print('%-20s\t' % j, end="")
print('')
elif condition == '2':
handle = ['1.ID', '2.类别', '3.账户', '4.金额', '5.时间', '6.说明']
for name in handle:
print('%-5s\t' % name, end="")
print('')
select = input('请输入要查找的条件序号[1-6]\n')
if select == '1':
id = int(input('请输入ID:'))
idCount = data.count(id)
if idCount == 0:
print('查无此ID')
return
idIndex = data.index(id)
endData = data[idIndex:idIndex + 6]
printTitle()
for j in endData:
print('%-20s\t' % j, end="")
print('')
elif select == '2':
category = input('请输入类别:')
categoryCount = data.count(category)
if categoryCount == 0:
print('查无此类别')
return
elif categoryCount == 1:
categoryIndex = data.index(category)
cData = [data[categoryIndex - 1]] + data[categoryIndex:categoryIndex + 5]
printTitle()
for i in cData:
print('%-20s\t' % i, end="")
print('')
else:
indexList = [index1 for index1,value1 in enumerate(data) if value1 == category]
printTitle()
for i in indexList:
cDataM = [data[i-1]] + data[i:i + 5]
for j in cDataM:
print('%-20s\t' % j, end="")
print('')
elif select == '3':
account = input('请输入账户:')
accountCount = data.count(account)
if accountCount == 0:
print('查无此账户')
return
elif accountCount == 1:
accountIndex = data.index(account)
cData = data[accountIndex - 2:accountIndex] + data[accountIndex:accountIndex + 4]
printTitle()
for i in cData:
print('%-20s\t' % i, end="")
print('')
else:
indexList = [index1 for index1,value1 in enumerate(data) if value1 == account]
printTitle()
for i in indexList:
cDataM = data[i-2:i] + data[i:i + 4]
for j in cDataM:
print('%-20s\t' % j, end="")
print('')
elif select == '4':
money = input('请输入金额:')
moneyCount = data.count(money)
if moneyCount == 0:
print('查无此金额')
return
elif moneyCount == 1:
moneyIndex = data.index(money)
cData = data[moneyIndex - 3:moneyIndex] + data[moneyIndex:moneyIndex + 3]
printTitle()
for i in cData:
print('%-20s\t' % i, end="")
print('')
else:
indexList = [index1 for index1,value1 in enumerate(data) if value1 == money]
printTitle()
for i in indexList:
cDataM = data[i-3:i] + data[i:i + 3]
for j in cDataM:
print('%-20s\t' % j, end="")
print('')
elif select == '5':
time = input('请输入时间:')
timeCount = data.count(time)
if timeCount == 0:
print('查无此时间')
return
elif timeCount == 1:
timeIndex = data.index(time)
cData = data[timeIndex - 4:timeCount] + data[timeIndex:timeIndex + 2]
printTitle()
for i in cData:
print('%-20s\t' % i, end="")
print('')
else:
indexList = [index1 for index1,value1 in enumerate(data) if value1 == time]
printTitle()
for i in indexList:
cDataM = data[i-4:i] + data[i:i + 2]
for j in cDataM:
print('%-20s\t' % j, end="")
print('')
elif select == '6':
state = input('请输入说明:')
stateCount = data.count(state)
if stateCount == 0:
print('查无此说明')
return
elif stateCount == 1:
stateIndex = data.index(state)
cData = data[stateIndex - 5:stateIndex] + [data[stateIndex]]
printTitle()
for i in cData:
print('%-20s\t' % i, end="")
print('')
else:
indexList = [index1 for index1,value1 in enumerate(data) if value1 == state]
printTitle()
for i in indexList:
cDataM = data[i-5:i] + [data[i]]
for j in cDataM:
print('%-20s\t' % j, end="")
print('')
else:
print('您的输入有误!')
return
print('-' * 150)
# 管理员界面菜单
def menuM():
print('{}管理员您好!'.format(manager[0]))
while True:
handle = ['1.添加账务', '2.编辑账务', '3.删除账务', '4.查询账务', '5.退出系统']
for name in handle:
print('%-5s\t' % name, end="")
print('')
select = input('请输入要操作的功能序号[1-5]\n')
if select == '1':
# 添加
add()
elif select == '2':
# 编辑
update()
elif select == '3':
# 删除
delete()
elif select == '4':
# 查询
query()
elif select == '5':
# 退出
print('退出系统,欢迎下次再来~')
sys.exit()
else:
print('您的输入有误!')
continue
# 普通用户界面菜单
def menuU():
print('{}用户您好!'.format(user[0]))
while True:
# 查询
query()
showDown = input('按Q/q退出系统,按其他键继续\n').lower()
if showDown == 'q':
print('退出系统,欢迎下次再来~')
sys.exit()
# 登录
def login():
i=2
while i >= 0:
account = input('请输入您的账号:')
password = input('请输入您的密码:')
if account == manager[0]:
if password == manager[1]:
# 进入管理员界面
menuM()
else:
print('您输入的密码有误!您还有{}次机会'.format(i))
elif account == user[0]:
if password == user[1]:
# 进入普通用户界面
menuU()
else:
print('您输入的密码有误!您还有{}次机会'.format(i))
else:
print('您输入的账号有误!您还有{}次机会'.format(i))
i -= 1
login()