Reputation: 39
I am working to create a quiz on Python with different difficulty levels. I am trying to use functions so I can keep calling the most frequent part of the code, appending the questions into a list from an external text file.
However, I am running into a problem. When attempting to call my function I get the error:
NameError: name 'f' is not defined
I've tried everything I can think of but if anyone could provide any help, it would be greatly appreciated!
Here is the function:
def quiz(f):
f = open("quiz.txt", "r").read().split('\n')
for line in f:
f.append(questions)
f.close()
quiz(f)
print(questions)
The print(questions)
bit is just a way for me to check if the lines have appended to the list.
Upvotes: 1
Views: 30478
Reputation: 51000
When your code hits the line quiz(f)
, no f
has been defined.
At that point in your code, only one thing has happened -- a function name quiz
has been defined. That's the only identifier you can refer to at that point in your code.
You have declared function quiz
to accept a parameter, but it makes no use of that parameter. The correct definition would be def quiz()
.
Upvotes: 3