尝试遍历文件并有问题



我有一个文件"game。txt"它由一个指令和一个数字组成。指令要么是"硬币",要么是"硬币"。"跳,"或者"没有!"Coin"存储指令"跳转&quot后的数字;将跳转到一个相对于自身的新指令并执行该指令所指示的任何操作,并且& none"什么也不做。然而,"jump +2"将继续执行下面两行指令,并"跳转-5";导致上面5行指令下一个执行。

我希望能够遍历文件,在新文件上写入数字,并计算有多少个"硬币"。在最后。我已经有了一个不错的函数,它让我有点接近这个,但是我有一些我似乎无法弄清楚的bug。

交货)。

  • 我有533作为我的总数,但只有528项在我的新文件
  • 我也想简化代码,如果可能的话(在某些部分看起来冗余)

game.txt文件game.txt文件link

def counting_coins(file):

count = 0
game_list = []  # list of all game steps
valid_coins = []  # list of all coin values
try:
coins = open("coins.txt", "x")
except FileExistsError:
coins = open("coins.txt", "w")  # if file already exists

with open(file, "r") as cc:
### LOOPS ###
for line in cc:
game_list.append(line[0:-1])  # append each line to list to index and iterate
for i in range(len(game_list)):
current = game_list[i]  # keep track of current step
if "coin" == current[0:4]:
count += 1
if game_list[i][5:6] == "+":
valid_coins.append(current[6:] + "n")
# count += 1
elif game_list[i][5:6] == "-":
valid_coins.append("-" + current[6:] + "n")
# count += 1
elif "jump" == current[0:4]:
if current[5:6] == "+":
num = int(current[6:])
jump = game_list[i + num]
elif current[5:6] == "-":
num = int(current[6:])
num = -num
jump = game_list[i + num]
if "coin" == jump[0:4]:
count += 1
if jump[5:6] == "+":
valid_coins.append(jump[6:] + "n")
# count += 1
elif jump[5:6] == "-":
valid_coins.append("-" + jump[6:] + "n")
# count += 1
elif "jump" == jump[0:4]:
if jump[5:6] == "+":
new_num = int(jump[6:])
new_jump = game_list[(i + num) + new_num]
elif jump[5:6] == "-":
new_num = int(jump[6:])
new_num = -new_num
new_jump = game_list[(i + num) + new_num]
if "coin" == new_jump[0:4]:
count += 1
if new_jump[5:6] == "+":
valid_coins.append(new_jump[6:] + "n")
# count += 1
elif new_jump[5:6] == "-":
valid_coins.append("-" + new_jump[6:] + "n")
# count += 1
elif "jump" == new_jump[0:4]:
if new_jump[5:6] == "+":
new_num2 = int(new_jump[6:])
new_jump2 = game_list[(i + num) + new_num + new_num2]
elif new_jump[5:6] == "-":
new_num2 = int(new_jump[6:])
new_num2 = -new_num2
new_jump2 = game_list[(i + num) + new_num + new_num2]
if "coin" == new_jump2[0:4]:
count += 1
if new_jump2[5:6] == "+":
valid_coins.append(new_jump2[6:] + "n")
# count += 1
elif new_jump2[5:6] == "-":
valid_coins.append("-" + new_jump2[6:] + "n")
# count += 1
elif "none" == current[0:4]:
continue
for i in range(len(valid_coins)):
if valid_coins[i] == valid_coins[-1]:  # if last entry
coins.write(valid_coins[i][:-1])  # removes preceding newline
else:
coins.write(valid_coins[i])
coins.close()
return coins, count

file, count = counting_coins("game.txt")
print(f"Total coins collected: {count}")

不要使用所有嵌套的if语句进行跳转。只需将主循环的当前索引重置为你要跳转到的元素。

你需要使用while循环,而不是在range()上循环,这样你就可以重置索引。

代替所有的切片,使用split()将每行拆分为命令和参数。

i = 0
while i < len(game_list):
current = game_list[i].split()
if current[0] == "coin":
count += 1
valid_coins.append(current[1])
elif current[0] == "jump"
i += int(current[1])
elif current[0] = "none":
pass
else:
print(f"invalid line {game_list[i]}")
with open("coins.txt", "w") as coins:
coins.write("n".join(valid_coins))

不需要try/except。在w模式下打开会创建一个不存在的文件,你不需要x模式。

最新更新