我正在学校学习一门课程,在Codio上有一个挑战:
在本单元的最后一个挑战中,您将加载两个文件:
第一个文件F1将包含有关某些帐户的信息。它将以管道分隔,每行有一条记录,包括以下字段:
ACCOUNT NUMBER | PIN CODE | BALANCE
第二个文件F2将包含指令:每行一条。说明如下所示:
COMMAND | AMOUNT | ACCOUNT NUMBER | PIN CODE
COMMAND
将是add或sub。如果命令是add,则将金额添加到帐户文件F1中的余额中。如果命令为sub,则将进行减法
但是,您可能需要出于多种原因拒绝交易。如果要求您减去一笔金额,该金额将使帐户低于零,或者如果您提供的pin码与帐户记录中的pin码不匹配,则该交易将被忽略
账户交易
给定管道分隔文件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
有人能告诉我哪里出了问题吗?谢谢
您的代码非常混乱,我建议您删除所有代码并从空文件重新启动:
以下几行没有意义:
如果您知道如何使用字典,它们可能会有所帮助:
这里有一些可能的解决方案类型的伪代码:
假设金额和余额是整数值
对于float,将代码中的int(…)更改为float(…)
代码
测试
跑步前
跑步后
我认为问题可能来自以下方面:
从我看到的情况来看,如果
records[i][1] != records2[i][3]
,它仍然运行elif
和减法运算相关问题 更多 >
编程相关推荐