Search code examples
pythoninputintegerreadfile

Function is not being called after input assigns a value (Python)


I have a small file with a bunch of phrases on each line. I want the user to type a number and that number will print the selected line.

def printSpecLine(x):
    print('started')
    with open('c:/lab/save.txt') as f:
        for i, line in enumerate(f, 1):
            if i == x:
                break
    print (line)
    print('done')
    f.close()

s = int(input("Enter a number: "))
printSpecLine(s)

I've ran this with no errors, but the function isn't being called at all. Printing "started" (second line) didn't even occur. Am I missing a step here?


Solution

  • The only explanation for this is that you are not actually inputting to the prompt! There doesn't seem to be any other reason why at least the first print wouldn't be made.

    Remember that input() is blocking, so until you enter your number and press enter, the program will be halted where it is (i.e. not call the function).