Wednesday, 15 April 2015

class - name is no defined in python -



class - name is no defined in python -

i want check in python whether instance of class (student) exists:

def student_exist(stuno): # check if pupil exist a='ris'+str(stuno) try: except nameerror: homecoming false else: homecoming true

when running existing sample, works fine. when come in non-existing stu. number, instead of "false" following:

traceback (most recent phone call last): file "", line 1, in nameerror: name 'ris1112' not defined

what miss here?

this seems bad design, seem having global variable every pupil , checking if variable exists.... perhaps should doing like

students = ["r1", "r2", "r3"] # etc etc def student_exist(stuno): homecoming ("r%s" % stuno) in students

python class instance nameerror

No comments:

Post a Comment