Python Why is Python Telling Me My Variable is Not Defined?

AI Thread Summary
The discussion revolves around a Python programming issue where a variable, 'masterlist', is reported as undefined. The user has a function, 'getData', that successfully creates 'masterlist' but does not return it, leading to the undefined error in the 'usernameAndPasswordInput' function. A suggestion is made to return 'masterlist' from 'getData', but the user still encounters the same error after implementing this change. The importance of using code tags for better readability is also highlighted. The main focus is on resolving the scope issue of the 'masterlist' variable to ensure it is accessible in the relevant functions.
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

 
Technology news 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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
18
Views
3K
Replies
2
Views
2K
Replies
7
Views
1K
Replies
14
Views
34K
Replies
6
Views
4K
Replies
1
Views
3K
Back
Top