Python ATM function implementation code example

Write ATM program to achieve the following functions, the data comes from the file db.txt

1、 Recharge function: the user enters the recharge amount, and the account amount in db.txt is modified

2、 Transfer function: user A transfers 1,000 yuan to user B, and user A account deduction is completed in db.txt, and user B account adds money

3、 Withdrawal function: the user enters the amount of withdrawal, the amount of money in the account in db.txt is reduced

4、 Inquiry balance function: enter account number to inquire balance

Login function

After the user logs in successfully, the status is recorded in the memory. The above functions are subject to the current login status. You must log in before you can operate

code show as below

import os
user_staus ={'username': None}
def ad_credit(username, amount):"""
Recharge function
: param username::param amount::return:"""
if user_staus['username'] is None:login()withopen('db.txt','rt', encoding='utf-8')as f1, \
open('db.txt.swap','wt', encoding='utf-8')as f2:while True:
cont = f1.readline()iflen(cont)==0:break
name, remain = cont.strip().split(':')if username in name:
remain =int(remain)+int(amount)
f2.write('{}:{}\n'.format(name, remain))print('The recharge is successful,{}The balance is{}'.format(username, remain))else:
f2.write(cont)
os.remove('db.txt')
os.rename('db.txt.swap','db.txt')
def transfer(user_out, user_in, amount):'''3
Transfer function
: param user_out::param user_in::param amount::return:'''
if user_staus['username'] is None:login()withopen('db.txt','rt', encoding='utf-8')as f1, \
open('db.txt.swap','wt', encoding='utf-8')as f2:
userinfo ={}for line in f1:
name, remind = line.strip().split(':')
userinfo[name]=int(remind)if user_out not in userinfo:print('User does not exist')returnif user_in not in userinfo:print('The payee does not exist')returnif user_out in userinfo and user_in in userinfo:if userinfo[user_out]=int(amount):
userinfo[user_out]-=int(amount)
userinfo[user_in]+=int(amount)print('The transfer was successful, and the{}to{}money transfer{}'.format(user_out, user_in, amount))
elif userinfo[user_out]< amount:print('Insufficient balance')returnfor name, remind in userinfo.items():
f2.write('{}:{}\n'.format(name, remind))
os.remove('db.txt')
os.rename('db.txt.swap','db.txt')
def cashon(username, amount):'''
Withdrawal function
: param username::param amount::return:'''
if user_staus['username'] is None:login()withopen('db.txt','rt', encoding='utf-8')as f1, \
open('db.txt.swap','wt', encoding='utf-8')as f2:
userinfo ={}for line in f1:
name, remind = line.strip().split(':')
userinfo[name]=int(remind)if username not in userinfo:print('User does not exist')returnif username in userinfo and userinfo[username]=int(amount):
userinfo[username]-=int(amount)print('Withdrawn from balance{}, The current balance is{}'.format(amount, userinfo[username]))
elif userinfo[username]< amount:print('Insufficient balance, withdrawal failed')returnfor name, remind in userinfo.items():
f2.write('{}:{}\n'.format(name, remind))
os.remove('db.txt')
os.rename('db.txt.swap','db.txt')
def check(username):'''
Balance query function
: param username::return:'''
if user_staus['username'] is None:login()withopen('db.txt','rt', encoding='utf-8')as f:
userinfo ={}for line in f:
name, remind = line.strip().split(':')
userinfo[name]= remind
if username not in userinfo:print('User does not exist')returnif username in userinfo:print('The current balance is:{}'.format(userinfo[username]))
def login():
username =input('Enter your user name')
userpassword =input('enter password')withopen('login.txt','rt', encoding='utf-8')as login_f:
login ={}for line in login_f:
name, psd = line.strip().split(':')
login[name]= psd
if username in login:if login[username]== userpassword:print('Successful landing')
user_staus['username']= username
break
elif username not in login:print('Username does not exist')return
def logout():
user_staus['username']= None
print('Signed out successfully')returnlogin()
tag = True
while tag:
cmd =input('''
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
''') if cmd =='1':
username =input('Enter your user name:')
amount =input('Enter the recharge amount:')ad_credit(username, amount)
elif cmd =='2':
user_out =input('Please enter the transfer party:')
user_in =input('Please enter the recipient:')
amount =input('Enter the transfer amount:')transfer(user_out, user_in, amount)
elif cmd =='3':
username =input('Enter your user name:')
amount =input('Enter the withdrawal amount')cashon(username, amount)
elif cmd =='4':
username =input('Enter your user name:')check(username)
elif cmd =='0':logout()
tag = False
else:print('Please enter the serial number correctly')

The following is the simulation result

”’
/Users/chenfeng/PycharmProjects/ATM/venv/bin/python /Users/chenfeng/PycharmProjects/ATM/main.py
Enter username xilou
Enter password 666
Successful landing
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
1
Enter username: xilou
Enter the recharge amount: 200
The recharge is successful and the balance of xilou is 700
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
2
Please enter the transfer party: xilou
Please enter the recipient: heiren
Enter the transfer amount: 200
The transfer is successful, and 200 has been successfully transferred from xilou to heiren
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
3
Enter username: xilou
Enter the withdrawal amount 100
100 has been withdrawn from the balance and the current balance is 400
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
4
Enter username: xilou
The current balance is: 400
Please enter the serial number of the function you want to use
1 : Top up
2 : Transfer
3 :withdraw
4 :Check balances
0 :Sign out
0
Signed out successfully
Process finished with exit code 0

"'The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python ATM function implementation code example
Python drawing rose implementation code
Python regular expression example code
Python tcp transmission code example analysis
Python requests module session code example
Python verification code interception identification code example
Python object-oriented example
Python enumerate() function
Python function buffer
Python implementation of AI automatic matting example analysis
Python implementation of hand drawing effect example sharing
Python custom function basics
Python3.7 debugging example method
Python built-in function -compile()
Python function basic learning
Python data analysis-apply function
Python3 built-in function table.md
Python SMS bombing code
Python interpolate interpolation example
Python Print print timer function
Python high-order function usage summary!
Python code to find bugs (2)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python negative modulus operation example
Python3 logging log package example
Python implementation of gomoku program
Python tornado upload file function
Python implements code block folding
Python output mathematical symbols example
Python magic function eval () learning
Python install OpenCV sample code
Python code to find bugs(6)
How Python implements FTP function
Python iterable object de-duplication example
Python code to find bugs (1)
Python code to find bugs(8)
Python implements verification code recognition
Python code to find bugs(5)
Python one-dimensional two-dimensional interpolation example
Is python code case sensitive
Python GUI simulation implementation calculator
Python draw bar graph (bar graph) example
Python right alignment example method
Python implements image stitching function
Python high-order function usage summary!