Python ATM模拟系统
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python ATM模拟系统相关的知识,希望对你有一定的参考价值。
花了半天把2.x的语法改成了3.x,补齐了部分功能
先说下遇到的几个区别:
1.print
2.x里 print "Hello World!!"
3.x里 print("Hello World!!!")
2.input
2.x里面用 raw_input()
3.x里只有 input()
3.字典的has_key()方法,python3以后删除了has_key()方法
2.x dic.has_key(‘xxx‘)
3.x key in dic
然后是思路,因为老师要求数据要永久保存,所以我用了两个txt文本,在程序启动前读入文本内容,在程序执行完毕后,将字典内容写入文本,代码如下
其他功能大概都是常规思路
#coding=utf-8 import os #read txt into dic User_Pwd = {} User_Money={} def open_User_Pwd(): if(os.path.exists(‘User_Pwd_file.txt‘)): User_Pwd_file = open(‘User_Pwd_file.txt‘, ‘r‘) for line in User_Pwd_file: line = line.strip().split(‘:‘) User_Pwd[line[0]]=line[1] User_Pwd_file.close() else: User_Pwd_file = open(‘User_Pwd_file.txt‘, ‘w‘) User_Pwd_file.close() def open_User_Money(): if(os.path.exists(‘User_Money_file.txt‘)): User_Money_file = open(‘User_Money_file.txt‘, ‘r‘) for line in User_Money_file: line = line.strip().split(‘:‘) User_Money[line[0]] = line[1] User_Money_file.close() else: User_Money_file=open(‘User_Money_file.txt‘,‘w‘) User_Money_file.close() # write User-password into txt def Write_User_Pwd(): User_Pwd_file = open(‘User_Pwd_file.txt‘,‘w‘) for key in User_Pwd: User_Pwd_file.write(key +":" + User_Pwd[key]+‘\n‘) User_Pwd_file.close() #write User-Money into txt def Write_User_Money(): User_Money_file=open(‘User_Money_file.txt‘,‘w‘) for key in User_Money: User_Money_file.write(key +":" +str(User_Money[key])+‘\n‘) User_Money_file.close() def MainMeun(): open_User_Pwd() open_User_Money() print("=====欢迎使用蜗牛ATM无限制存取款系统=====") print("===请输入选项:1.登陆 2.注册 3.退出===") print("=========================================") choice =input() if choice==‘1‘: print("=====欢迎来到登录模块=====") login() elif choice==‘2‘: print("=====欢迎来到注册模块=====") register() elif choice==‘3‘: print("=====你即将退出系统=====") exit() else: print("=====请输入正确的选项=====") MainMeun() #login def login(): UserName = input("请输入您的账号:").strip() n=0 t=0 while (n<2): if (not UserName in User_Pwd): n=n+1 print ("对不起,请重新输入") UserName = input("请输入您的账号:").strip() else: if(t>2): exit() else: UserPassword = input("请输入您的密码 :").strip() if(not User_Pwd[UserName]==UserPassword): t=t+1 print ("对不起,请重新输入") else: global CurrentName CurrentName = UserName level_two() break #level_two def level_two(): print ("请输入选项") print("1.查询余额 \n" "2.转账\n" "3.取现\n""4.存钱\n" "5.回到主菜单\n""6.退出" ) choice2=input() if (choice2==‘1‘): print("=====查询余额=====") CheckBalance() elif(choice2==‘2‘): print ("=====转账=====") MoneyTransfer() elif(choice2==‘3‘): print("=====取现=====") getcash() elif(choice2==‘4‘): print("=====存钱=====") savecash() elif(choice2==‘5‘): print("=========================================") MainMeun() elif(choice2==‘6‘): exit() else: print("请输入正确的选项:") level_two() #1.check the balance def CheckBalance(): UserName =input("请输入账户:") if (UserName in User_Money): print("当前账户是 " + UserName) print ("当前余额是 " + User_Money[UserName]) print ("-------------------------------------------------------------------") level_two() else: print("请输入正确的账号") CheckBalance() #2.转账 def MoneyTransfer(): print("当前的账户是 " + CurrentName) TransferName=input("请输入要转入的账号: ") if(TransferName in User_Money and not CurrentName==TransferName): TransferMoney=input("请输入要转入的金额: ") if(TransferMoney.isdigit() and int(User_Money[CurrentName])>=int(TransferMoney)): User_Money[CurrentName]= int(User_Money[CurrentName])-int(TransferMoney) User_Money[TransferName]=int(User_Money[TransferName])+int(TransferMoney) Write_User_Money() print(CurrentName + "余额是 " + str(User_Money[CurrentName])) print(TransferName+ "余额是 " + str(User_Money[TransferName])) level_two() else: print("!!!!!!!!!!!!!请输入正确的金额!!!!!!!!!!!") MoneyTransfer() else: print("!!!!!!!!!!!!!!请输入正确的用户名!!!!!!!!!!!!!!!") MoneyTransfer() #取现 def getcash(): print("当前账号是:"+ CurrentName) print("当前的余额是:"+str(User_Money[CurrentName])) num = input("请输入取现金额:") if(num.isdigit()): if(int(User_Money[CurrentName])>=int(num) and int(num)>0): User_Money[CurrentName]=int(User_Money[CurrentName])-int(num) Write_User_Money() print("取现后剩余金额:" + str(User_Money[CurrentName])) level_two() else: print("余额不足!!!") else: print("请输入数字!!!") #存钱 def savecash(): print("当前账号是:" + CurrentName) print("当前的余额是:" + str(User_Money[CurrentName])) num = input("请输入存入金额:") if (num.isdigit() and int(num) > 0): User_Money[CurrentName] = int(User_Money[CurrentName]) + int(num) Write_User_Money() print("存入后剩余金额:" + str(User_Money[CurrentName])) level_two() else: print("输入错误!!!") print(‘---------------------------------------------------------‘) level_two() #register def register(): UserName=input("请输入用户名:") if UserName in User_Pwd: print("用户名已存在,请重新输入") register() else: UserPwd=input("请输入密码:") User_Pwd[UserName] = UserPwd User_Money[UserName]=int(5000) print("恭喜你,注册成功") print("Your User Name is " + UserName) print ("You have money:$5000") Write_User_Pwd() Write_User_Money() MainMeun() MainMeun()
以上是关于Python ATM模拟系统的主要内容,如果未能解决你的问题,请参考以下文章