Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about NameError
#1
Hi, I am learning Python object oriented programming through this Youtube video. I am following along with his code. However, I am getting a "NameError" where I cannot pin-point where my error is in my code. I triple checked word-for-word of my code with the Youtuber's code and it matches up, but I am not sure why I am getting an error and he is not. Any help or suggestions would be greatly appreciated. Thanks!

class Student:
    def __init__(self, name, age, grade):
        self.name = name 
        self.age = age
        self.grade = grade
    
    def get_grade(self):
        return self.grade

class Course:
    def __init__(self, name, max_students):
        self.name = name
        self.max_students = max_students
        self.students = []

    def add_student(self, student):
        if len(self.students) < self.max_students:
            self.students.append(student)
            return True
        return False
    
    def get_average_grade(self):
        pass

s1 = Student("Tim", 19, 95)
s2 = Student("Bill", 19, 75)
s3 = student("Jill", 19, 65)

course = Course("science", 2)
course.add_student(s1)
course.add_student(s2)
print(course.students)
Reply
#2
line 27 student needs to be titlecase.

there's more:
s1, s2, and s3 are classes of Student, they are not strings.
Therefore you must treat then as such.

You are too liberal with the use of name student and Student (which are two different objects)
Than999 likes this post
Reply
#3
(Dec-10-2021, 03:53 PM)Larz60+ Wrote: line 27 student needs to be titlecase.

Ok, great! Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner question NameError amazing_python 6 2,478 Aug-13-2021, 07:28 AM
Last Post: amazing_python
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 4,419 Oct-27-2019, 07:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020