Help:python loop, beginner, condition

Hello, I need some help with my loop. It should print out the number when four numbers after each other are not equal. Unfortunately I think there is something wrong my conditions… because it always gives the answer 7. Total is a integer of different numbers…

with open(‘Day6.in’) as file:
data = [i for i in file.read().strip()]

L=list(range(0,len(data)))

current=
total=

for entry in data:
current = ord(entry)
total.append(current)
continue

total_sum = 0
for entry, i in zip(total,L):
if i < (len(L)-4):
    if ([total[i] != total[i+1] !=total[i+2] != total[i+3]] == True):
        total_sum += 4
        break
    else:
        total_sum += 1
        continue
else:
break

print(total_sum)

Hello, I need some help with my loop.

Hi. Please surround your entire code in code fences; it looks like you
just put them for the code from total_sum through to the break. I’ll
reprint the code as I think it was intended below.

Please check it and make sure it matches your code, and report any
inconsistencies:

 with open('Day6.in') as file:
     data = [i for i in file.read().strip()]

 L=list(range(0,len(data)))

 current=[]
 total=[]

 for entry in data:
     current = ord(entry)
     total.append(current)
     continue

 total_sum = 0
 for entry, i in zip(total,L):
     if i < (len(L)-4):
         if ([total[i] != total[i+1] !=total[i+2] != total[i+3]] == True):
             total_sum += 4
             break
         else:
             total_sum += 1
             continue
     else:
         break

 print(total_sum)

It should print out the number when four numbers after each other are
not equal. Unfortunately I think there is something wrong my
conditions… because it always gives the answer 7. Total is a integer
of different numbers…

Can you show the contents if the Day6.in file? I suspect it is text
with a number per line. But that’s just a guess. Post the contents
between code fences as well.

On the assumption that you file just has a number per line, let’s look
at your code:

 with open('Day6.in') as file:
     data = [i for i in file.read().strip()]

This reads the entrie file as one string (file.read()), stripes
leading and trailing whilespace from that string, the makes a list of
every character in the string. So each i value will be a single
character string from the file. For example, if the file contained:

 1
 2
 3

The “single string” above would be '1\n2\n3\n' and after the
.strip() it would be '1\n2\n3'. You’d get a list like this:

 ['1', '\n', '2', '\n', '3']

You can check this by adding a print() call after reading the file:

 print('data =', data)

You should do that now. Seeing what’s in data will help you debug
further problems. Until data contains what you intend it to contain,
there’s no point fixing further problems.

I think what you want is an actual int value like 1, 2, 3in the list indata. So what you _should_ do, if so, is read the file line by line, and store the number from each line in data`. Like this:

 with open('Day6.in') as file:
     data = []
     for line in file:
         data.append(int(line.strip()))

Quickly glancing at the rest of the code:

 L=list(range(0,len(data)))

So a list of numbers from 0 through len(data)-1.

 current=[]

Why is current a list?

 total=[]

 for entry in data:
     current = ord(entry)

I suspect this ord() is to turn the single character strings from
data into an integer. This gets you the ordinary values of each
character in current in turn. So for my example data above, the values
49, 10, 50, 10, 51, 10. If you change the code to read lines
and make ints as above, you’ll already have 1, 2, 3 in data.

     total.append(current)
     continue

You don’t need this continue - you’re already at the bottom of the
loop and it’ll commence the next iteration automatically.

 total_sum = 0
 for entry, i in zip(total,L):
     if i < (len(L)-4):
         if ([total[i] != total[i+1] !=total[i+2] != total[i+3]] == True):

This test is probably the root cause of your trouble. You’ll gain a
little insight by printing it, by changing this:

 if ([total[i] != total[i+1] !=total[i+2] != total[i+3]] == True):

into:

 test_result = ([total[i] != total[i+1] !=total[i+2] != total[i+3]] == True)
 print("test-result =", test_result)
 if test_result:

What you’ve written is evaluationed like this:

 total[i] != total[i+1] != total[i+2] != total[i+3]

which you turn into a single element list (your outer [] brackets),
and you then compare the list to True, which will always be false
because a list is not equal to any Boolean.

You probably just want this:

 test_result = total[i] != total[i+1] !=total[i+2] != total[i+3]

Add print() calls in your code to show important values, it helps
figure out what’s actually going on.

Cheers,
Cameron Simpson cs@cskk.id.au