我是编码新手,我对这个问题的说明是:“给定以管道分隔的文件 F1 和 F2,其中 F1 包含具有字段 ACCOUNT NUM|PIN|BALANCE 的帐户,F2 包含交易指令 COMMAND|AMOUNT|ACCOUNT NUM|PIN,执行交易,将结果存储回 F1。COMMAND 字段将是 add 或 sub 指示从帐户中添加或减去。不提供正确 PIN 码或试图将帐户置于零以下的交易应被忽略。”我的输出是:1000|1234|100001020|2222|03000|3344|10002020|1234|90000正确的输出是:1000|1234|110001020|2222|03000|3344|02020|1234|90000我写的代码是:import sysF1= 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: breakprint(records)
添加回答
举报
0/150
提交
取消