python写ATM机功能
话不多说上代码!
1 user_list = [{'user': 'zhangsan', 'password': '111111', 'balance': 1000}, 2 {'user': 'lisi', 'password': '222222', 'balance': 2000}, 3 {'user': 'wangwu', 'password': '333333', 'balance': 3000}] 4 current_user = None 5 6 7 def reg(): 8 print('这是一个注册功能!') 9 while True: 10 un = input('请输入您的用户名:') 11 for item in user_list: 12 if un.strip() == item['user']: 13 print('您输入的用户名已存在,请重新输入') 14 break 15 else: 16 pw = input('请输入您的密码:') 17 if len(pw) < 6: 18 print('密码不能小于6位!') 19 else: 20 user_list.append({'user': un, 'password': pw, 'balance': 3000}) 21 print('注册成功!') 22 return True 23 24 25 def login(): 26 print('这是一个登录功能!') 27 while True: 28 un = input('请输入您的用户名:') 29 pw = input('请输入您的密码:') 30 for user in user_list: 31 if user['user'] == un and user['password'] == pw: 32 print('恭喜你登录成功!') 33 global current_user 34 current_user = user 35 print('当前登录用户为:', current_user['user']) 36 return 37 else: 38 print('用户名或密码错误!') 39 40 41 def check_current(): 42 if current_user: 43 print('当前余额为:',current_user['balance']) 44 else: 45 print('请先登录再进行查询操作!') 46 47 48 def deposit(): 49 if current_user: 50 money = input('请输入您要存款的金额:') 51 if money[-2:] == '00' and len(money) > 2 and int(money) > 0: 52 current_user['balance'] += int(money) 53 print('存款成功!') 54 else: 55 print('您输入的金额有误!') 56 else: 57 print('请先登录再进行存款操作!') 58 59 60 def withdrawal(): 61 if current_user: 62 money = input('请输入你要取款的金额:') 63 if money[-2:] == '00' and len(money) > 2 and int(money) > 0: 64 if int(money) > current_user['balance']: 65 print('余额不足!') 66 else: 67 current_user['balance'] -= int(money) 68 print('取款成功!') 69 else: 70 print('您输入的金额有误!') 71 else: 72 print('请先登录再进行存款操作!') 73 74 75 def transfer(): 76 if current_user: 77 to_user = input('请输入需要转账的账户名:') 78 if to_user == current_user['user']: 79 print('不能给自己转账!') 80 else: 81 for user in user_list: 82 if to_user == user['user']: 83 money = input('请输入需要转账的金额:') 84 if money[-2:] == '00' and len(money) > 2 and int(money) > 0: 85 if int(money) > current_user['balance']: 86 print('余额不足!') 87 else: 88 current_user['balance'] -= int(money) 89 user['balance'] += int(money) 90 print('转账成功!') 91 else: 92 print('您输入的金额有误!') 93 break 94 else: 95 print('您输入的账户不存在!') 96 else: 97 print('请先登录再进行转账操作!') 98 99 100 def get_menu(): 101 menu = ''' 102 ***请选择您要操作的菜单*** 103 **1.注册 2.登录 3.查询余额 4.存款 5.取款 6.转账 7.退卡** 104 ''' 105 while True: 106 print(menu) 107 op = input('请输入您的选择:') 108 if op == '1': 109 reg() 110 elif op == '2': 111 login() 112 elif op == '3': 113 check_current() 114 elif op == '4': 115 deposit() 116 elif op == '5': 117 withdrawal() 118 elif op == '6': 119 transfer() 120 elif op == '7': 121 print('您已成功退出!请取走您的卡片。') 122 break 123 else: 124 print('无此选项!') 125 126 127 get_menu()
本文来自博客园,作者:wujin啊~,转载请注明原文链接:https://www.cnblogs.com/wujina/p/16067263.html

浙公网安备 33010602011771号