Why is Python Telling Me My Variable is Not Defined?

Click For Summary

Discussion Overview

The discussion revolves around a Python programming issue where a user encounters an error indicating that a variable is not defined. The focus is on understanding variable scope and function returns within the context of a larger program.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • The user, Katie, describes a problem where Python reports that the variable 'masterlist' is not defined within a loop in the 'usernameAndPasswordInput' function.
  • Some participants suggest that the 'getData' function should return the 'masterlist' variable to make it accessible outside the function.
  • Katie reports that even after modifying the function to return 'masterlist', the error persists, indicating that 'masterlist' is still considered an undefined global variable.
  • Another participant requests that Katie use code tags to preserve formatting for better clarity in the code shared.

Areas of Agreement / Disagreement

Participants have not reached a consensus on the solution to the problem, as Katie continues to experience the same error despite suggested changes. There are competing views on how to resolve the issue related to variable scope.

Contextual Notes

There may be limitations related to the scope of variables and how they are accessed across different functions, which has not been fully explored in the discussion.

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?
 

Similar threads

  • · Replies 43 ·
2
Replies
43
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
1K
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 14 ·
Replies
14
Views
35K
  • · Replies 0 ·
Replies
0
Views
2K