Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your final challenge in this unit, you will load two files. The first file F1 will have information about some accounts. It will be

For your final challenge in this unit, you will load two files. The first file F1 will have information about some accounts. It will be pipe-delimited and have one record per line, with these fields:

ACCOUNT NUMBER | PIN CODE | BALANCE

The second file F2 will contain instructions: one on each line. The instructions will look like this:

COMMAND | AMOUNT | ACCOUNT NUMBER | PIN CODE

COMMAND will be either add or sub. If the command is add, you will add AMOUNT to the BALANCE in the account files F1. If the command is sub, you will subtract.

However, there are a number of reasons for which you may need to reject the transaction. If you are asked to subtract an amount that would put the account below zero or if the pin code you are provided does not match the pin code in the account record, the transaction is ignored.

Account Transactions

Given pipe-delimited files F1 and F2 where F1contains accounts with fields ACCOUNT NUM|PIN|BALANCE and F2 contains transaction instructions COMMAND|AMOUNT|ACCOUNT NUM|PIN, execute the transactions, storing the results back in F1.

The COMMAND field will be add or sub indicating addition or subtraction from the account.

Transactions which do not provide the correct PINcode or attempt to put the account below zero should be ignored.

THIS IS MY CODE AND IT IS WRONG:

# Get the filepath from the command line import sys F1= sys.argv[1] F2= sys.argv[2]

# Your code goes here # Your code goes here lines1 = [] lines2 = []

with open(F1) as fh: lines1 = fh.readlines()

with open(F2) as fh: lines2 = fh.readlines()

accounts = {} account_list = [] for line in lines1: (account, pin, balance) = line.split("|") accounts[account] = [pin, int(balance)] account_list.append(account)

commands = []

for line in lines2: (command, amount, account, pin) = line.split("|") amount = float(amount) if account in accounts: if command == "add" and accounts[account][0] == pin: accounts[account][1] += amount elif command == "sub" and accounts[account][0] == pin and (accounts[account][1] - amount >= 0): accounts[account][1] -= amount

with open(F1, "w") as fw: for account in account_list: pin = accounts[account][0] amount = str(accounts[account][1]) fw.write(account + "|" + pin + "|" + amount + " ")

THIS IS THE OUTPUT FOR MY CODE AND THE EXPECTED OUTPUT. PLEASE CORRECT MY CODE IT NEEDS TO MATCH THE EXPECTED OUTPUT. ALSO PROVIDE THE CORRECT INDENTATION PLEASE!

image text in transcribed

Check It LAST RUN on 2/18/2018, 3:50:46 PM Program Output Your program output did not match the expected output. Your output: 100011234/10000 1020/222210 30001334411000 2020/1234190000 Expected output 1000/1234111000 10201222210 30001334410 202011234190000

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions