模拟ATM
userlist = [
{"name": "zhangsan", "password": 562390, "money": 500},
{"name": "lisi", "password": 123456, "money": 1000},
{"name": "wangwu", "password": 897456, "money": 2000}
]
#验证用户名密码是否匹配
def User_Login(username, pwd):
for i in range(len(userlist)):
if userlist[i]['name'] == username and userlist[i]['password'] == pwd:
return 1
while True:
print("欢迎进入中国银行")
print("1. 登陆", "2. 退出","3. 余额","4. 存款","5. 取款")
key = input("请输入选择:")
if key == "1":
username = input("请输入账户:")
pwd = input("请输入密码:")
if UserLogin(username, int(pwd))==1:
print('登陆成功')
else:
print ("登陆失败")
if key == "2":
break
input("按回车键继续:")
#已经定义了用户列表,存款及取款的操作能够和输入的用户名相对应,存款及取款能够影响到余额,逻辑不需严谨,只需简单实现出来即可。我是初学者 小白
userlist = [
{"name": "zhangsan", "password": 562390, "money": 500},
{"name": "lisi", "password": 123456, "money": 1000},
{"name": "wangwu", "password": 897456, "money": 2000}
]
item = {"a":"存款","b":"取款","c":"余额","d":"注销"}
#验证用户名密码是否匹配
def UserLogin(username, pwd):
for user in userlist:
if user['name'] == username and user['password'] == pwd:
return 1
while True:
print("欢迎进入中国银行")
print("1. 登陆 "+"2. 退出")
key = input("请输入选择:")
if key == "1":
username = input("请输入账户:")
pwd = input("请输入密码:")
if UserLogin(username, int(pwd))==1:
print('登陆成功')
while True:
for key in sorted(item.keys()):
print(key+":"+item[key])
key = input("请输入选择:")
if key == "a":
cash = input("请输入要存款的数额:")
for user in userlist:
if user["name"] == username:
user["money"] = user["money"] + int(cash)
input("存款成功,回车请继续")
break
elif (key == "b"):
while True:
exitflg = False
cash = input("请输入要取款的数额:")
for user in userlist:
if user["name"] == username:
if user["money"] < int(cash):
print("存款额度小于取款额度,请重新输入")
continue
user["money"] = user["money"] - int(cash)
input("取款成功,回车请继续")
exitflg = True
break
if exitflg:
break
elif (key == "c"):
for user in userlist:
if user["name"] == username:
print("当前余额:"+str(user["money"]))
break
elif (key == "d"):
break;
else:
print("输入错误,请重新输入")
else:
print ("登陆失败")
if key == "2":
break
input("按回车键继续:")
https://blog.csdn.net/ccccc49813645075/article/details/80547283