0

我正在通过学​​校学习一门课程,并在 Codio 上遇到了这个挑战:

对于本单元的最后一个挑战,您将加载两个文件:
第一个文件 F1 将包含有关某些帐户的信息。它将以竖线分隔,每行有一条记录,包含以下字段:

ACCOUNT NUMBER | PIN CODE | BALANCE

第二个文件 F2 将包含说明:每行一个。说明将如下所示:

COMMAND | AMOUNT | ACCOUNT NUMBER | PIN CODE

COMMAND将是添加或子。如果命令为 add,您将在帐户文件 F1 中将 AMOUNT 添加到 BALANCE。如果命令是 sub,您将减去。

但是,您可能需要拒绝交易的原因有很多。如果您被要求减去一笔金额以使账户低于零,或者您提供的密码与账户记录中的密码不匹配,则该交易将被忽略。

账户交易给定管道分隔的文件 F1 和 F2,其中 F1 包含带有字段的账户,ACCOUNT NUM|PIN|BALANCEF2 包含交易指令COMMAND|AMOUNT|ACCOUNT NUM|PIN,执行交易,将结果存储回 F1。

COMMAND字段将是添加或子,表示从帐户中添加或减去。

应忽略未提供正确 PIN 码或试图将帐户置于零以下的交易。

这是我的挑战代码:

records = []

with open(F1,'r') as account_info:
  content = account_info.readlines()
  for row in content:
    recordList = row.strip("\n").split('|')
    records.append(recordList)

records2 = []
with open(F2,'r') as user_input:
  content2 = user_input.readlines()
  for row in content2:
    recordList2 = row.strip("\n").split('|')
    records2.append(recordList2)

for i in range(len(records)):
  row = records[i]
for i in range(len(records2)):
  row = records2[i]

for row in records and records2:
  if records[i][1] == records2[i][3] and records2[i][0] == "add":
    newBalance = int(records[i][2]) + int(records2[i][1])
    records[i][2] = str(newBalance)
  elif records2[i][0] == "sub" and int(records[i][2]) >= int(records2[i][1]):
    newBalance = int(records[i][2]) - int(records2[i][1])
    records[i][2] = str(newBalance)


output_records = ""
i = 0
while i <= len(records):
  output_records += '|'.join(records[i])
  if i != len(records):
    output_records += '\n'
    i += 1
    if i == len(records):
      break

outputFile = open(F1, 'w')
outputFile.write(output_records)
outputFile.close

这就是我得到的输出,输出相差一个数字。

Your program output did not match the expected output.

Your output:
1000|1234|10000
1020|2222|0
3000|3344|0
2020|1234|90000

Expected output:

1000|1234|11000
1020|2222|0
3000|3344|0
2020|1234|90000

有人可以指出我出错的方向吗?谢谢。

4

3 回答 3

0

假设金额和余额是整数值。

对于代码中的 float 将 int(...) 更改为 float(...)

代码

# Get Records
with open('file1.txt','r') as f1:
  records = []
  for row in f1:
    row = row.rstrip().split('|')
    # Strip white space and convert balance to float
    row = [x.strip() if i != 2 else int(x.strip()) for i, x in enumerate(row)]
    records.append(row)

# Get Transactions
with open('file2.txt', 'r') as f2:
  transactions = []
  for row in f2:
    row = row.rstrip().split('|')
    # Strip whitespace and convert balance to float
    row = [x.strip() if i != 1 else int(x.strip()) for i, x in enumerate(row)]
    transactions.append(row)

# Perform Transactions
for t in transactions:
  for record in records:
    # check records for matching account & pin
    # Brute force search -- okay for records and transactions only in thousands
    if t[2:] == record[:2]: 
      # Found account to update (record account & pin matches transaction)
      if t[0] =='add':
        record[-1] += t[1]  # increment balance
      elif t[0] == 'sub':
        if record[-1] - t[1] >= 0:
          record[-1] -= t[1]  # decrement balance
      break

# Output updated records
with open('file1.txt', 'w') as f3:
  for row in records:
    row = [str(x) for x in row]
    f3.write(' | '.join(row) + '\n')

测试

跑步前

File1.txt
1000 | 1234 | 10000
1020 | 2222 | 2500
3000 | 3344 | 3000
2020 | 1234 | 95000

File2.txt
add    | 1000    | 1000 | 1234
sub    | 1000    | 1020 | 2222
add    | 1000    | 3000 | 3344
sub    | 1000    | 2020 | 1234

运行后

File1.txt
1000 | 1234 | 11000
1020 | 2222 | 1500
3000 | 3344 | 4000
2020 | 1234 | 94000
于 2020-06-07T23:52:39.727 回答
-1

你的代码真的很乱,我可以建议你删除所有并从一个空文件重新启动:

以下几行毫无意义:

for row in records and records2:


for i in range(len(records)):
   row = records[i]
for i in range(len(records2)):
   row = records2[i]

如果您知道如何使用字典,它们可能会有所帮助:

这里有一些可能的解决方案的伪代码:

accounts = {}
with open(F1,'r') as f:
    for line in f:
        acc, pin, balance = line.split('|')
        accounts[acc] = {'pin': pin, 'balance': int(balance)}


with open(F2,'r') as f:
    for line in f:
        command, amount, acc, pin = line.split('|')
        amount = int(amount)
        if accounts[acc]['pin'] != pin:
            continue # wrong pin
        if command == 'add':
            accounts[acc]['balance'] += amount
        elif accounts[acc]['balance'] >= amount: # if there is enough balance to sub
            accounts[acc]['balance'] -= amount
于 2020-06-07T22:25:53.573 回答
-1

我认为问题可能来自这些:

for row in records and records2:
  if records[i][1] == records2[i][3] and records2[i][0] == "add":
    newBalance = int(records[i][2]) + int(records2[i][1])
    records[i][2] = str(newBalance)
  elif records2[i][0] == "sub" and int(records[i][2]) >= int(records2[i][1]):
    newBalance = int(records[i][2]) - int(records2[i][1])
    records[i][2] = str(newBalance)

据我所见,如果records[i][1] != records2[i][3]它仍然运行elif并减去。

于 2020-06-07T22:03:56.437 回答