我是编码新手,我对这个问题的说明是:“给定以管道分隔的文件 F1 和 F2,其中 F1 包含具有字段 ACCOUNT NUM|PIN|BALANCE 的帐户,F2 包含交易指令 COMMAND|AMOUNT|ACCOUNT NUM|PIN,执行交易,将结果存储回 F1。
COMMAND 字段将是 add 或 sub 指示从帐户中添加或减去。
不提供正确 PIN 码或试图将帐户置于零以下的交易应被忽略。”
我的输出是:
1000|1234|10000
1020|2222|0
3000|3344|1000
2020|1234|90000
正确的输出是:
1000|1234|11000
1020|2222|0
3000|3344|0
2020|1234|90000
我写的代码是:
import sys
F1= sys.argv[1]
F2= sys.argv[2]
records=[]
with open(F1,'r') as fp:
content=fp.readlines()
for row in content:
recList=row.strip("\n").split('|')
records.append(recList)
records2=[]
with open(F2,'r') as fp:
content=fp.readlines()
for row in content:
recList2=row.strip("\n").split('|')
records2.append(recList2)
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]:
if records2[i][0]=="add":
records[i][2]=int(records[i][2])+int(records2[i][1])
elif records2[i][0]=="sub":
if int(records[i][2])>=int(records2[i][1]):
records[i][2]=int(records[i][2])-int(records2[i][1])
else:
records[i][2]=records[i][2]
else:
break
print(records)
慕雪6442864
相关分类