0

I am trying to do the Advent Of Code 2022, 1st problem. (DONT TELL THE ANSWER). What i am doing is reading the file and taking each number and adding it to a sum value. What happens is, when I come across the "\n", it doesn't understand it and I am having trouble trying to create the array of sums. Can anyone help?

`

with open("input.txt") as f:
  list_array = f.read().split("\n")
  print(list_array)
  new_array = []
  sum = 0
  for i in list_array:
    print(i)
    if i == "\n":
      new_array.append(sum)
      sum = 0
    sum += int(str(i))
    print(sum)

`

I was trying to convert to back to a str then an int, but it doesn't work

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563

1 Answers1

0

You can check if i is an integer or not using check=i.isnumeric(). Put an if condition:

for i in list_array:
    if (check==True):
        sum+=i
        new_array.append(sum)
AlexK
  • 2,855
  • 9
  • 16
  • 27
  • 1
    Don't use `if variable == True:`, use `if variable:` (see [pep8 recommendations](https://peps.python.org/pep-0008/#programming-recommendations)). Also don't use the built-in `sum` function name as a variable name. – AlexK Dec 08 '22 at 07:08