I
I
isusloff2021-11-07 00:56:57
Python
isusloff, 2021-11-07 00:56:57

ValueError: invalid literal for int() with base 10: '', what should I do?

Here is part of the code:

with open('28128_A.txt', 'r') as f:
    n = int(f.readline())
    for i in range(n):
        a = int(f.readline())
        for i in range(n-1):
            b = int(f.readline())
            print(a, b)

6186f8ee3b9af302279145.png
Here is a file with numbers, in the first line the number n is the number of trace numbers, I need to compare each number with each to find such a sum of two numbers that they are divisible by three, but an error occurs, it seems to me that ''a'' after comparing all the numbers, b goes into some line, but I don’t understand where and how and why.
Or maybe there is some simpler way to write the task?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-11-07
@galaxy

f.readline() reads the next line of the file. Count how many lines you read in your code (hint: more than n)
Read all lines into a list and then compare as you wish

W
Wataru, 2021-11-07
@wataru

Enter into a variable and display it on the screen so that you can see what is wrong with it.
Wherever you read from a file do:

l = f.readline()
print("The line is: \""+l+"\"")
n = int(l)

Then it will immediately become obvious what is wrong with the file or code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question