Reputation: 474
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 :/
Upvotes: 1
Views: 221
Reputation: 1833
This is because classes
is out of scope for the second two methods. Therefore, you have two options:
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
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
Upvotes: 1
Reputation: 2481
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.
Upvotes: 2
Reputation: 180391
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():
Upvotes: 1
Reputation: 5682
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.
Upvotes: 5