1

Whenever I try to run this code:

    #Open file
    f = open("i.txt", "r")
    line = 1

    #Detect start point
    def findstart( x ):
        length = 0
        epsilon = 7
        a = 3
        line_value = int(f.readline(x))
        if line_value == a:
            length = length + 1
            x = x + 1
            findend(x)
        elif line_value == epsilon:
            x = x + 2
            findstart(x)
        else:
            x = x + 1
            findstart(x)

    #Detect end point
    def findend(x):
        line_value = int(f.readline(x))
        if line_value == a:
            length = length + 1
            return ("Accept", length)
        elif line_value == epsilon:
            x = x + 2
            length = length + 2
            findend(x)
        else:
            x = x + 1
            length = length + 1
            findend(x)

    findstart(line)

I get this error code:

    Traceback (most recent call last):
  File "C:\Users\Brandon\Desktop\DetectSequences.py", line 39, in <module>
    findstart(line)
  File "C:\Users\Brandon\Desktop\DetectSequences.py", line 16, in findstart
    findend(x)
  File "C:\Users\Brandon\Desktop\DetectSequences.py", line 26, in findend
    line_value = int(f.readline(x))
    ValueError: invalid literal for int() with base 10: ''

Can anyone help me to figure out what's wrong? It seems to me that it's attempting to read an empty cell but I don't know why that would be. The file I'm scanning currently only has two lines with each reading "3" so it should output a success but I can't get past this error.

1
  • What is this code trying to do? Commented Nov 2, 2011 at 0:09

3 Answers 3

2

I'm not sure about your code, but the error message suggests that your file has an empty line in it and you're trying to convert it to an int. For example, a lot of text files have an empty line at the end.

I'd suggest first checking your line before converting it:

line = ...
line = line.strip() # strip whitespace
if line: # only go on if the line was not blank
    line_value = int(line)
Sign up to request clarification or add additional context in comments.

Comments

1

You're reading a blank line, and python doesn't like that. You should probably be checking for blank lines.

line_value = f.readline(x).strip()
if len(line_value) > 0:
    line_value = int(line_value)
    ...

1 Comment

if line: is a more pythonic way to write it, instead of using len.
1

You have a scope issue with the variables a, length and epsilon. You define it in findstart, but try to access it in findend.

Also, the variable x being passed to readline is not doing what you think. Readline always returns the next line in the file, the variable passed to it is an optional hint of what the length of the line might be, it is not which line should be read. To operate on specific lines, read the entire file in to a list first:

# Read lines from file
with open("i.txt", "r") as f:
    # Read lines and remove newline at the end of each line
    lines = [l.strip() for l in f.readlines()]

    # Remove the blank lines
    lines = filter(lambda l: l, lines)

EPSILON = 7
A = 3
length = 0

#Detect start point
def findstart( x ):
    global length

    length = 0

    line_value = int(lines[x])
    if line_value == A:
        length += 1
        x += 1
        findend(x)
    elif line_value == EPSILON:
        x += 2
        findstart(x)
    else:
        x += 1
        findstart(x)

#Detect end point
def findend(x):
    global length

    line_value = int(lines[x])
    if line_value == A:
        length += 1
        return ("Accept", length)
    elif line_value == EPSILON:
        x += 2
        length += 2
        findend(x)
    else:
        x += 1
        length += 1
        findend(x)

findstart(0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.