Python--作业3--模拟ATM程序的流程
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Huanglinsheng import pymysql db = pymysql.connect("localhost","root","112233","TESTDB" ) cursor = db.cursor() sql = '''CREATE TABLE IF NOT EXISTS userinfoes ( user VARCHAR(20), password VARCHAR(6), age VARCHAR(3), sex VARCHAR(5), mobile VARCHAR(11), balance FLOAT(12,2))''' try: cursor.execute(sql) db.commit() except: db.rollback() sql = '''INSERT INTO userinfoes(user,password,age,sex,mobile,balance) VALUES ('luu','123456','19','woman','12345678900',10000)''' try: cursor.execute(sql) db.commit() except: db.rollback() def login(): print("Input your name:") k = 3 while k: global username username = input() userinfoes = ("select * from userinfoes WHERE user='%s'" %username) j = cursor.execute(userinfoes) if username == '': print("用户名不能为空!") elif j == 0: k -=1 if k !=0: print("用户名错误,请重新输入:") else: print("你已经输错三次了,已经被锁定!") db.close() cursor.close() exit() else: break print("请输入你的密码:") k = 3 while k: password = input() userinfoes = ("select * from userinfoes WHERE user='%s' and password='%s'" %(username,password)) j = cursor.execute(userinfoes) if password == '': print("密码不能为空!") elif j == 0: k -=1 if k != 0: print("密码错误,请重新输入:") else: print("输入三次错误,账户被锁定!") db.close() cursor.close() exit() else: print("恭喜你,登陆成功") break def enroll(): username = input(print("请输入用户名(长度<=20):")) while 1: userinfoes = "select * from userinfoes WHERE user='%s'" %username j = cursor.execute(userinfoes) if j == 1: print("用户已经存在,请重新输入!") elif username == '': print("用户名不能为空") else: break def judge(): while(1): password = input() if len(password) != 6: print("密码长度不够,要六位以上!") else: break return password while 1: print("请输入密码(长度=6):") p = judge() print("请再次确认密码:") q = judge() if p != q: print("两次密码不一样,请重新输入") else: break age = input("请输入你的年龄:") while 1: if age == "": print("年龄不能为空,请重新输入!") elif len(age)>3: print("请输入有效年龄!") else: break sex = input("请输入你的性别(man/woman):") while 1: if sex != 'man' and sex != 'woman': print("请输入正确的性别") else: break mobile = input('请输入你的电话号码:') while 1: j = 0 for i in mobile: if i<'0' or i>'9': j +=1 if len(mobile) != 11 or j !=0: print("请输入有效信息!") else: break sql = "INSERT INTO userinfoes(user,password,age,sex,mobile,balance)VALUES('%s','%s','%s','%s','%s','%f')" % (username, p, age, sex, mobile, 5000.00) try: cursor.execute(sql) db.commit() print('注册成功,获得5000元余额!') judge1() except: db.rollback() db.close() cursor.close() def Home_page(): print('''======欢迎使用蜗牛ATM无限制存取款系统====== ====请输入你的选项,1:登录 2:注册 3:退出=== ===========================================''') def judge1(): Home_page() while 1: judge_1 = input() if judge_1 == '1': login() judge2() elif judge_1 == '2': enroll() elif judge_1 == '3': print("感谢你的使用!") db.close() cursor.close() exit() else: print("请输入有效数字") def Home_page_2(): print('''====================请输入你的选项===================== ==1:查询余额 2:转账 3:取款 4:存款 5:返回主菜单 6:退出== =======================================================''') def judge2(): while 1: Home_page_2() judge_2 = input() if judge_2 == '1': print("你的余额为:%0.2f" %user_balance(username)) continue elif judge_2 == '2': while 1: other_username = input("请输入对方用户名:") userinfoes = "SELECT * FROM userinfoes WHERE user='%s'" % other_username # 查询该用户是否存在 j = cursor.execute(userinfoes) if other_username == '': print("用户名不能为空!") elif j == 0: print("用户名不存在,请输入有效用户名;") else: sum = float(input("请输入转账金额:")) M_balance = user_balance(username) O_balance = user_balance(other_username) if M_balance - sum >= 0: sq1 = "UPDATE userinfoes SET balance=%0.2f WHERE user='%s'" % (M_balance - sum, username) sq2 = "UPDATE userinfoes SET balance=%0.2f WHERE user='%s'" % (O_balance + sum, other_username) try: cursor.execute(sq1) cursor.execute(sq2) db.commit() print("转账成功!") except: db.rollback() print("转账失败") else: print("转账失败,你余额不足!") break continue elif judge_2 == '3': sum = float(input("请输入你的取款金额:")) M_balance = user_balance(username) if M_balance - sum >= 0: sq1 = "UPDATE userinfoes SET balance='%0.2f' WHERE user='%s'" % (M_balance - sum, username) try: cursor.execute(sq1) db.commit() print("取款成功!") except: db.rollback() else: print("你的余额不足!") continue elif judge_2 == '4': sum = input("请输入你的存款金额(金额<100,000,000.00):") if ',' in sum: sum = sum.replace(',','') sum = float(sum) sql = "UPDATE userinfoes SET balance=balance+'%0.2f' WHERE user='%s'"%(sum, username) try: cursor.execute(sql) db.commit() print( "存款成功!") except: db.rollback() print("存款失败!") continue elif judge_2 == '5': judge1() elif judge_2 == '6': print("感谢你的使用!") db.close() cursor.close() exit() else: print("请输入有效数字!") continue break def user_balance(h): userinfoes = "SELECT * FROM userinfoes WHERE user='%s'" %h j = cursor.execute(userinfoes) k = cursor.fetchmany(j) for i in k: return i[5] judge1()
posted on 2018-08-01 15:54 huanglinsheng 阅读(246) 评论(0) 编辑 收藏 举报