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?
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
I am trying to run an .ipynb file and have installed Miniconda as well as created an environment as such -conda create -n <env_name> python=3.7 ipykernel jupyter I am assuming this is successful as I can activate this environment via the anaconda prompt and following command -conda activate <env_name> Then I downloaded and installed VS code and I am trying to edit an .ipynb file. I want to select a kernel, via VS Code but when I press the button on the upper right corner I am greeted...

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