1

I am trying to make a program that can add/delete/show students in a class, and the 5 classes are 5 lists in a list.

Help is greatly appreciated.

When I run this code:

global classes
def intro():
    print("Welcome to Powerschool v2.0!")
    print("Actions:")
    print("1. Add Student")
    print("2. Delete Student")
    print("3. Show Students in a Class")
    print("4. Show All Students")
    x = int(input())
    while x<1 or x>4:
        print ("Please choose an action, 1-4.")
        x = int(input())
    if x == 1:
        action1()
    elif x == 2:
        action2()
    elif x == 3:
        action3()
    elif x == 4:
        action4()
    classes = [[],[],[],[],[]]
    return classes
def action1():
    print("Which Class? 1-5")
    a = int(input())
    print("Please enter the student's name.")
    z = input()
    classes[a-1].append(z)
    again()
def action2():
    print ("Which Class? 1-5")
    print ("Which student?")
    again()
def action3():
    print ("Which Class? 1-5")
    y = int(input())
    if y == 1:
        print (classes[0])
    elif y == 2:
        print (classes[1])
    elif y == 3:
        print (classes[2])
    elif y == 4:
        print (classes[3])
    elif y == 5:
        print (classes[4])
    again()
def action4():
    print (classes)
    again()
def again():
    print("Would you like to do something else? y/n")
    h = input()
    if h == "y":
        intro()
    else:
        quit
def main():

    intro()

main()

My error is:

Traceback (most recent call last):
  File "C:\Documents and Settings\user1\My Documents\Downloads\az_studenttracker.py", line 67, in <module>
    main()
  File "C:\Documents and Settings\user1\My Documents\Downloads\az_studenttracker.py", line 65, in main
    intro()
  File "C:\Documents and Settings\user1\My Documents\Downloads\az_studenttracker.py", line 19, in intro
    action1()
  File "C:\Documents and Settings\user1\My Documents\Downloads\az_studenttracker.py", line 33, in action1
    classes[a-1].append(z)
NameError: name 'classes' is not defined

I did return classes at the end of intro() but I see that doesn't work. I followed some suggestions, and nothing really happened :/

2
  • You are going out of scope when classes[a-1].append(z) because for that case, your classes haven't been defined. You are only getting classes after def intro() method. Commented Nov 15, 2014 at 20:54
  • Also i now realize I have no idea what return does. :/ Commented Nov 15, 2014 at 21:10

4 Answers 4

5

You're defining classes in your intro method, and, even though it's returning it, your action1 method doesn't see any variable named classes anywhere.

Relevant answer on Python scope and relevant documentation.

Sign up to request clarification or add additional context in comments.

Comments

2

return doesn't do what you think it does. return statements are a way of passing execution control back up a context (For example, from intro() to main()), with the ability to send back some information for the higher context to use. Although you're passing classes back to main(), you never do anything with it at that context so it goes away.

One way to solve the problem would be to declare classes as a global variable. This is the easiest thing to do, but isn't generally good design. You could do this either by using the global keyword before declaring the local variable classes in intro() (See this question for guidance on global), or by declaring classes outside any of your functions.

Another solution would be to pass classes as a parameter to your action functions.

In either case, you would need to declare classes before any calls to your action functions.

Comments

1

This is because classes is out of scope for the second two methods. Therefore, you have two options:

Option 1

Pass classes to the methods action1(), action2(), etc like so:

def action1(classes)

...and the when you call it:

action1(classes) //with the classes var you just made

Option 2 (recommended)

Simply put the classes var outside your methods or declare it global like so:

global classes = [[],[],[],[],[]]

...right before:

def intro()

In general, you should read up on how return works; it is not necessary in the code you wrote

4 Comments

It doesn't make sense to declare a variable as global when it's already at the global scope. You would use global on the local variable classes in intro() to make it globally-scoped without having to declare it there
I don't get this scope thing other than the fact that global is used across the whole program? and local is within that one def
@Player72 correct. That is all you need to know as far as scope is concerned in this problem
@Player72 No problem. You should read through this, it will help you considerably in grasping some concepts: tutorialspoint.com/python
1

classes only exists in intro():, you would have to declare it as a global variable to access it in other functions or declare it outside the function.

classes = [[],[],[],[],[]] # can be accessed by action3() ,action4()
def intro():

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.