0

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.

2
  • 2
    You need to refer to it as student.StudentInfo. Alternatively, change the import to from student import * (or from student import StudentInfo if that's all you need). Commented Oct 14, 2020 at 0:52
  • 1
    @TomKarzes Although wildcard imports should be avoided in most scenarios since they pollute the namespace. Commented Oct 14, 2020 at 0:53

2 Answers 2

1

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.

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

Comments

0

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.

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.