What is the problem with this code?


#1

Hi,
I am only writing following code to run that, but I find NameError: name 'report' is not defined and NameError: name 'menu' is not defined. What is the problem?

class student:

def v(self):
    self.x=input("enter name:")
    self.y=int(input("enter #:"))
    self.y=int(input("stnumber:"))

def report(y):
    i=0
    while i<len(st):
        st[i].display()
        i+=1   

def menu():
    print()
    print("1.add")
    print("2.max")
    print("3.list")
    print("4.find")
    print("5.remove")
    print("6.exit")
    ch=int(input("enter choice 1 to 6"))
    return ch

st=[]
st1=student()
while True:
c=int(input("enter c:"))
if c==1:
st1.v()
st.append(st1)
elif c==2:
y=int(input("enter #:"))
report(y)


#2

Hello @simanii,

Since you have defined report and menu inside of the class student, you need to use the dot notation with them. For instance:

class Student:
    def __init__(self):
        pass
    def method(self):
        return "hi"

student_1 = new Student()

student_1.method() #correct
method(student_1) #incorrect

Hope this clears out your doubts...


#3

Thanks,
I am a beginner so that can you please explain a bit more? I am not sure if I understood what is the problem?


#4

Sure, @simanii,

A function that is defined inside of a class, "belongs" to the class. It is called a method of that class. It perform some operation only of members of that class. That is why methods like .lower() or .upper() only work with strings, because they are functions defined inside of the string class.

You are using methods in the traditional sense. If you want to use a function without dot notation, remove it from the students class and define it outside. Then you will be able to call it like function_name(args) Hope this clears our your doubts.

PS: If you feel my answer was satisfactory, don't be afraid to put it as the solution so that others know it is closed


#5

Thanks,
I found what you wrote. Can you please write my program in both two ways that you mentioned inside and outside that class in correct way? I did not wrote such a code till know.


#6

You suppose the code is as follow:

class student:

def v(self):
    self.x=input("enter name:")
    self.y=int(input("enter #:"))
    self.y=int(input("stnumber:"))


def menu():
    print()
    print("1.add")
    print("2.max")
    print("3.list")
    print("4.find")
    print("5.remove")
    print("6.exit")
    ch=int(input("enter choice 1 to 6"))
    return ch

st=[]
st1=student()
while True:
c=menu()
if c==1:
st1.v()
st.append(st1)

Am I write in writing the form of defining inside the class?


#7

I would, @simanii, but according to Codecademy's Community Guidelines, I cannot write out your code for you, I can only assist you in finding out the solution. But I can give you an example.

class Random:
    def __init__(self):
        pass
    def method(self, arg):
        return arg*2

rand = new Random()
print rand.method(64) #prints 128

That is the function-in-class example.

def function(args):
    return args*2

num = 64
print function(num) #prints 128

As for your question, if you want your function to be applied to everything, then define it outside the class. But if you want it to operate on members of the class, then define it inside. Hope this clears out your problems...


#8

What is the function of init in this regard?


#9

I know what are you saying about the second example (function), but I cannot run your first example again like mine. I do not know what is the problem.


#10

The function of __init__ in this case is to initialise instances of the class in reference.


#11

Do you mean that function should have amount every time? I mean I should define amount for class in both examples and then it can run?


#12

OH!!!
I run this one:

class Random:
def init(self):
pass
def method(self, arg):
return arg*2

rand = Random()
print (rand.method(64)) #prints 128

it runs with and without :
def init(self):
pass
So, can you explain the use of int more?
My problem is eliminated, but please explain a bit about int and its use. As I could run program with and without that, what is its usage?


#13

Other question, can I write the function outside the class and use that as same as inside? you suppose your example:

def method( arg):
return arg*2

class Random:
def init(self):

rand = Random()
print (rand.method(64)) #prints 128

Is something like that workable?


#14

Sorry for the late reply @simanii, i had other matter to attend to.

Yes it is, however you well have to define the method inside the class as well. The function of __init__ is to initialise a member of a class. Without an __init__ you will not be able to define a member of the class.


#15

Thanks,
But how can I run your program with and without int and the results are same?


#16

Possibly because you haven't created any instances of the student class