【一起python】银行管理系统
文章目录
📝计算机基础概念
以下是对上述代码各部分的详细解析:
🌠 导入模块
import random
这行代码导入了Python的random
模块,用于生成随机的卡号。
🌠定义input_card_info
函数
def input_card_info():
name = input("请输入你的姓名:")
telphone = input("请输入你的手机号:")
idcard = input("请输入你的身份证号:")
mima = input("请输入你的密码:")
money = int(input("请输入你的预存金额:"))
kahao = str(random.randint(100000, 999999))
return kahao, mima, money, name, idcard, telphone
🌠 定义check_password
函数
def check_password(kahao, mima, yonghu_dict):
count = 0
while mima not in yonghu_dict.get(kahao, {}).values():
mima = input("你的密码错误,请重新输入:")
count += 1
if count == 3:
print("该卡号被锁定!")
return False
return True
False
;如果密码正确,则返回True
。🌠初始化用户字典和欢迎信息
yonghu_dict = {"卡号": {"密码": "123", "金额": "5"}}
print("欢迎使用本系统!")
yonghu_dict
,其中包含一个示例用户的信息(卡号、密码和金额)。🌉 主循环
while True:
print("1.开户 2.查询 3.取款 4.存款 5.转账 6.退出")
try:
num = int(input("请输入你要操作的序号:"))
except ValueError:
print("请输入正确的整数序号!")
continue
try - except
块来捕获用户输入不是整数时的ValueError
,如果发生错误,提示用户重新输入。🌉开户操作
if num == 1:
kahao, mima, money, name, idcard, telphone = input_card_info()
yonghu_dict[kahao] = {"密码": mima, "金额": money}
print("开户成功!")
print("卡号:", kahao)
print("密码:", mima)
print("姓名:", name)
print("余额:", money)
print("身份证:", idcard)
print("手机号:", telphone)
print(yonghu_dict)
num == 1
)时,调用input_card_info
函数获取用户信息,并将新用户信息添加到yonghu_dict
字典中。🌉查询操作
elif num == 2:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
print("你的余额为:", yonghu_dict[kahao]['金额'])
num == 2
)时,要求用户输入卡号和密码。check_password
函数检查密码是否正确。如果密码正确,打印该卡的余额。🌉取款操作
elif num == 3:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
a = int(input("请输入你要取款的金额:"))
if 0 < a <= yonghu_dict[kahao]['金额']:
print("取款成功!")
yonghu_dict[kahao]['金额'] -= a
else:
print("你要取款的金额大于你卡里的金额或取款金额小于等于0")
num == 3
)时,要求用户输入卡号和密码。check_password
函数检查密码是否正确。如果密码正确,要求用户输入取款金额。🌉存款操作
elif num == 4:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
a = int(input("请输入你要存款的金额:"))
if a > 0:
print("存款成功!")
yonghu_dict[kahao]['金额'] += a
else:
print("你要存款的金额小于等于0")
num == 4
)时,要求用户输入卡号和密码。check_password
函数检查密码是否正确。如果密码正确,要求用户输入存款金额。🌉 转账操作
elif num == 5:
kahao = input("请输入要转出卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
kahao1 = input("请输入要转入卡号:")
if kahao1 not in yonghu_dict:
print("卡号输入错误!")
continue
b = int(input("请输入要转出金额:"))
if b <= yonghu_dict[kahao]['金额'] and b > 0:
yonghu_dict[kahao]['金额'] -= b
yonghu_dict[kahao1]['金额'] += b
print("转账成功!")
print("转出卡卡里余额:", yonghu_dict[kahao]['金额'])
print("转入卡卡里余额:", yonghu_dict[kahao1]['金额'])
else:
print("你卡里的余额不足!")
num == 5
)时,要求用户输入转出卡号和密码。check_password
函数检查密码是否正确。如果密码正确,要求用户输入转入卡号。🌉退出操作
elif num == 6:
break
else:
print("请输入正确的操作序号!")
num == 6
)时,使用break
语句跳出循环,结束程序。🌠完整代码
import random
def input_card_info():
name = input("请输入你的姓名:")
telphone = input("请输入你的手机号:")
idcard = input("请输入你的身份证号:")
mima = input("请输入你的密码:")
money = int(input("请输入你的预存金额:"))
kahao = str(random.randint(100000, 999999))
return kahao, mima, money, name, idcard, telphone
def check_password(kahao, mima, yonghu_dict):
count = 0
while mima not in yonghu_dict.get(kahao, {}).values():
mima = input("你的密码错误,请重新输入:")
count += 1
if count == 3:
print("该卡号被锁定!")
return False
return True
yonghu_dict = {"卡号": {"密码": "123", "金额": "5"}}
print("欢迎使用本系统!")
while True:
print("1.开户 2.查询 3.取款 4.存款 5.转账 6.退出")
try:
num = int(input("请输入你要操作的序号:"))
except ValueError:
print("请输入正确的整数序号!")
continue
if num == 1:
kahao, mima, money, name, idcard, telphone = input_card_info()
yonghu_dict[kahao] = {"密码": mima, "金额": money}
print("开户成功!")
print("卡号:", kahao)
print("密码:", mima)
print("姓名:", name)
print("余额:", money)
print("身份证:", idcard)
print("手机号:", telphone)
print(yonghu_dict)
elif num == 2:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
print("你的余额为:", yonghu_dict[kahao]['金额'])
elif num == 3:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
a = int(input("请输入你要取款的金额:"))
if 0 < a <= yonghu_dict[kahao]['金额']:
print("取款成功!")
yonghu_dict[kahao]['金额'] -= a
else:
print("你要取款的金额大于你卡里的金额或取款金额小于等于0")
elif num == 4:
kahao = input("请输入卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
a = int(input("请输入你要存款的金额:"))
if a > 0:
print("存款成功!")
yonghu_dict[kahao]['金额'] += a
else:
print("你要存款的金额小于等于0")
elif num == 5:
kahao = input("请输入要转出卡号:")
mima = input("请输入密码:")
if kahao not in yonghu_dict:
print("卡号输入错误!")
continue
if check_password(kahao, mima, yonghu_dict):
kahao1 = input("请输入要转入卡号:")
if kahao1 not in yonghu_dict:
print("卡号输入错误!")
continue
b = int(input("请输入要转出金额:"))
if b <= yonghu_dict[kahao]['金额'] and b > 0:
yonghu_dict[kahao]['金额'] -= b
yonghu_dict[kahao1]['金额'] += b
print("转账成功!")
print("转出卡卡里余额:", yonghu_dict[kahao]['金额'])
print("转入卡卡里余额:", yonghu_dict[kahao1]['金额'])
else:
print("你卡里的余额不足!")
elif num == 6:
break
else:
print("请输入正确的操作序号!")
🚩总结
作者:阿森要自信