Why is Python Telling Me My Variable is Not Defined?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 3K views
katie_3011
Messages
5
Reaction score
0

Homework Statement


Hi. So I'm stuck on a few things right now, but the main one is that Python is telling me that one of my variables is not defined. I have stressed over this for about an hour now trying everything to change it, and I cannot seem to find issue.


Homework Equations





The Attempt at a Solution


My program is large, but shrinking it down to the relevant information:

def main ():
masterlist = getData("facepalm")
#Where getData is another function and facepalm is a textfile

def getData (filename):
textfile = open ("facepalm", "r")
user1 = [...]
#insert list of data from the textfile in order to separate the users from each other
user2 = [...]
masterlist = [[user1], [user2]]
textfile.close ()
#So this whole function works and gets the data
#Then the program proceeds to a login function where the user needs to enter the correct username and password

def usernameAndPasswordInput ():
username = input ("Enter your username: ")
password = input ("Enter your password: ")
for elmt in masterlist:
if elmt [0]==username:
if elmt [1]==password:
return masterlist
else:
...





So, the issue that I'm having is in the for loop which is where python tells me that the global name 'masterlist' is not defined.

Also, if you need me to send you my whole program via email, that's fine. I just didn't want to post all of it due to copy and paste programming.

Any help you can give me on this would be great, thanks.

Katie

 
on Phys.org
You can use [ code ] tags to preserve the whitespaces.

To me, it appears that your getData() function doesn't return the masterlist variable. Try adding
Code:
return masterlist
at the end of the function.
 
So I changed my function so that it returns masterlist at the end, and it still is saying that masterlist is an undefined global variable. Not sure what else to try...
 
Can you post the code in code tags so the spacing is preserved?