【问题标题】:So close yet I can't figure out whats wrong如此接近,但我无法弄清楚出了什么问题
【发布时间】:2020-06-07 21:45:58
【问题描述】:

我正在通过学​​校学习一门课程,并在 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|BALANCE 的帐户,F2 包含交易指令 COMMAND|AMOUNT|ACCOUNT NUM|PIN,执行交易,将结果存储回 F1。

COMMAND 字段将是 add 或 sub 表示从帐户中增加或减少。

应忽略未提供正确 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

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

【问题讨论】:

  • "如果您被要求减去一个金额,使账户低于零,或者如果您提供的密码与账户记录中的密码不匹配,则该交易将被忽略。"
  • 您在显示的代码中似乎没有处理这些条件
  • 我不知道该怎么做才能满足其他条件。
  • 您能否提供两个文件的输入数据以获取您得到的输出?
  • 我们没有得到输入数据。它已在文件中提供。

标签: python codio


【解决方案1】:

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

代码中的浮点数将 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

【讨论】:

    【解决方案2】:

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

    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 并减去。

    【讨论】:

      【解决方案3】:

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

      以下几行没有意义:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-03
        • 2018-05-09
        • 1970-01-01
        • 2017-08-28
        • 2015-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多