NoneType error when opening file
<p>So I've been trying to figure out why it's giving me this error. If I put this:</p>

<pre><code>def open_file():
    fp = open("ABC.txt")
    return fp

file = open_file()

count = 1

for line in file:
    if count == 9:
        line9 = line
    if count == 43:
        line43 = line
#blahblahblah more programming
</code></pre>

<p>This works, but this gives me NoneType object is not iterable:</p>

<pre><code>def open_file():
    while True:
        file = input("Enter a file name: ")
        try:
            open(file)
            break
        except FileNotFoundError:
            print("Error. Please try again.")
            print()

file = open_file()

count = 1

for line in file:  #here is where I get the error
    if count == 9:
        line9 = line
    if count == 43:
        line43 = line
</code></pre>

<p>I think it's just some silly mistake but I can't seem to find it.
Thanks for your time!</p>

<p>Your <code>open_file</code> function has no <code>return</code> statement, so it returns <code>None</code>. You should try something like</p>

<pre><code>def open_file():
    while True:
        file = input("Enter a file name: ")
        try:
            return open(file)
        except FileNotFoundError:
            print("Error. Please try again.")
            print()
</code></pre>

python nonetype