Crystal Young
Crystal Young

Reputation: 13

python returns nameError for class name not being defined

I am trying to code a program that calls on an established class from another python file called student. In the file student, a class called StudentInfo is established and init checks if the data is valid (eg. grade must be between 9-12, course code must fit format, etc.) I am trying to first take the user's inputs here.

import student
import transcript

def add_student(data):
    dataDict = data

    ID = str(len(dataDict) + 1)

    student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))

    return dataDict

When I try to define student as an object of class StudentInfo, it returns NameError: name 'StudentInfo' is not defined.

I'm not sure what I'm doing wrong. I thought it might be the inputs but when I removed them it seemed to do the same thing. Please help and thanks in advance.

Upvotes: 0

Views: 351

Answers (2)

Unsigned_Arduino
Unsigned_Arduino

Reputation: 366

It appears you forgot to prefix StudentInfo with student. You can either replace:

import student

With:

from student import StudentInfo

Or you can replace:

student = StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))

With:

student = student.StudentInfo(ID, input("Enter the student\'s last name: "), input("Enter the student\'s first name: "), input("Enter the student\'s grade: "), transcript.add_transcript(), input("Is the student registered: "))

On a side note: You shouldn't name variables after imports. Rename the variable student to something else.

Upvotes: 0

Carcigenicate
Carcigenicate

Reputation: 45736

You need student.StudentInfo if you're using import student.

Alternatively, you can import as:

from student import StudentInfo

To use the code that you have now.

Upvotes: 1

Related Questions