Why is Python Telling Me My Variable is Not Defined?

In summary, the conversation is about a person seeking help with a Python program. They are having trouble with a global variable not being defined in a for loop, even after making changes to their getData() function to return the variable. The conversation also includes a suggestion to use [code] tags to preserve the formatting of the code.
  • #1
katie_3011
5
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
  • #2
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.
 
  • #3
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...
 
  • #4
Can you post the code in code tags so the spacing is preserved?
 
  • #5




Hi Katie,

Thank you for reaching out for assistance with your Python programming assignment. It sounds like you are having trouble with a variable not being defined in your code. This can be a common issue and can be frustrating to troubleshoot. Here are a few things you can try to help resolve the issue:

1. Check for typos: Make sure that you are consistently using the same spelling and capitalization for your variable name. Python is case-sensitive, so a small typo can cause an error.

2. Scope of the variable: In your code, it looks like you have defined the masterlist variable in the getData() function. This means that it is only accessible within that function. In order to use it in the usernameAndPasswordInput() function, you will need to pass it as a parameter or make it a global variable.

3. Check for indentation errors: Python relies on indentation to determine the scope of functions and loops. Make sure that your code is properly indented, as a small mistake can cause unexpected results.

I hope these tips help you to resolve the issue. If you are still having trouble, please feel free to send me your entire program via email and I will take a closer look.

Best of luck with your assignment!

Sincerely,
 

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

1. What is Python programming?

Python is a high-level, interpreted, general-purpose programming language. It is widely used in various fields such as web development, data science, artificial intelligence, and more. It is known for its simple syntax, readability, and versatility.

2. What are the benefits of using Python for programming assignments?

Python is a popular choice for programming assignments because of its simplicity and ease of use. Its syntax is easy to understand and it has a large standard library which makes it suitable for a wide range of tasks. Additionally, Python has a supportive community and plenty of online resources for beginners to learn from.

3. Is Python suitable for beginners?

Yes, Python is considered to be one of the best programming languages for beginners. Its simple syntax and readability make it easy to learn and understand. Also, beginners can start coding with Python without having to worry about complex concepts such as memory management.

4. Can Python be used for more than just web development?

Yes, Python can be used for a variety of tasks such as data analysis, scientific computing, artificial intelligence, and more. It is a versatile language that can be used in many different fields and industries.

5. What are some resources for learning Python for programming assignments?

There are many resources available for learning Python, including online tutorials, video courses, books, and coding bootcamps. Some popular online resources for beginners include Codecademy, Coursera, and Udemy. It is also helpful to practice coding regularly and participate in online coding communities to improve your skills.

Similar threads

  • Programming and Computer Science
Replies
14
Views
31K
  • Programming and Computer Science
Replies
18
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
2
Replies
41
Views
4K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
883
  • Programming and Computer Science
Replies
2
Views
3K
Back
Top