For my assignment I have been asked to create a python ATM. I am going to have all the balances, account details, pin and account number on a txt file which I have made. How would I access this file then create a variable from the number which then can be used to deduct balances add balances and change the pin. All I have done so far is:
f = open('info.txt').read().split()
print(f)
And I am stuck on using those values to create variables in the actual program
The test in the file looks like
Accountnumber 123412341234
Accountpin 1234
Creditcardbal 0
Savingsbal 0
Name Alex
So how could I create a varieble called creditcardbal from the data above and if I want to change it how can I edit it in the file.
Firstly, you should not store the information in the manner that you have shown. One very efficient way of storing such information will be to use a csv (comma separated values) file. For your case, a csv file will look like below. Go ahead and store it in a file named atm.csv (you can change the extension if you wish.
Accountnumber,Accountpin,Creditcardbal,Savingsbal,Name
123412341234,1234,0,0,Alex
587452639810,5879,230.20,1265.88,Hans Muster
Go ahead and create a python file called atm.py with following code
import csv
global atm
class Account:
def __init__(self, anum, apin, crbal, savbal, name):
self.account_number = anum
self.account_pin = apin
self.credit_balance = float(crbal)
self.savings_balance = float(savbal)
self.name = name
def change_credit_balance(self, balance):
self.credit_balance = self.credit_balance + balance
def change_savings_balance(self, balance):
self.savings_balance = self.savings_balance + balance
def change_account_pin(self, pin):
self.account_pin = pin
def __str__(self):
return "name: %s, account_number: %s, credit_balance: %s, savings_balance: %s, account_pin: %s" \
%(self.name, self.account_number, self.credit_balance, self.savings_balance, self.account_pin)
atm = []
def increase_all_savings(amount):
for account in atm:
account.change_savings_balance(amount)
def print_all_accounts():
for account in atm:
print(account)
def load_data():
with open('atm.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
# for the first row which is column header
pass
else:
print('Values are %s', ",".join(row))
# data, one record per line
atm.append(Account(row[0], row[1], row[2], row[3], row[4]))
line_count += 1
def store_data():
with open('atm.csv', mode='w') as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# write header or column names
csv_writer.writerow(['Accountnumber','Accountpin','Creditcardbal','Savingsbal','Name'])
# write all records
for account in atm:
csv_writer.writerow([account.account_number, account.account_pin, account.credit_balance, account.savings_balance, account.name])
if __name__ == '__main__':
load_data()
print("=========")
print("Initial state")
print_all_accounts()
increase_all_savings(30)
print("=========")
print("State after increasing savings balance by 30")
print_all_accounts()
store_data()
execute this python file (assuming you have python3) like so
python3 atm.py
Follow the printed input, output and csv file content (before and after execution) carefully, you will get the idea for your assignment
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.