How can I access Pieter's grade after it has been added?

Also you can try:

def add_grade(self, grade):
    if type(grade) == Grade:
      self.grades.append(grade.score)

Happens that you are apppended the Grade object itself to the grade list in student, it should be the score attribute of Grade object

Wrong. We are not manipulating the object before it is appended. It is folly to give such advice. The post will be removed, presently.

Ok, but it seems i was not the only one that post a similar solution

We’ll give this thread a looking over before any further action. Thanks for raising that point.

I simply used a list comprehension to loop through pieter.grades and add to a list ‘pieters_grades_list’. Then just print that list

pieters_grades_list = [i.score for i in pieter.grades]
print(pieters_grades_list)

I am struggling with this one.

Do we need to create a method for printing? I though we could access to printing lists in another way. I don’t want a specific index, i want to print the whole list to check what is stored there before calculating average. Can anyone give me a hint? I mean…Create a method for printing is strictly needed?

Thanks in advance. :smiley:

Strictly needed, such dangerous terms in programming. There are often multiple ways we can achieve something. But certain solutions are certainly “better” then other solutions.

a method for printing would be a good way to solve this problem. Gives you readable, easy to re-use code.

1 Like

Ofc…I got that. But in previous lection we print lists normally right? Or is it because they are instances we have to access in special ways…Also is possible i am missing something?

I am going to read the whole thread. I realized were more interactions loading when you sroll down…Sorry for that.

Thanks again!

grades is instance variable, so we can access the list/instance variable:

print(pieter.grades)

but that is not very pretty. But it works

1 Like

I tried that the first but it shows me the space memory address as they pointed out others above instead of the grades scores as int in the list, that’s what i’m struggling with. I am trying to understand that grades contain Grade object who has inside score attribute and we should access score to obtain the scores as numbers but…until i get the mental puzzle solved i have some mess. It is seems easy but the OOP is a central topic…xdd better understand it properly hehe

Also i am trying to build the attendance situation but i have an error:

import datetime
class Student():
  def __init__(self, name, year):
    self.name = name
    self.year = year
    self.grades = []
    self.attendance ={}
  def add_grade(self, grade):
    if type(grade) is Grade:
      self.grades.append(grade)
  def get_average():
    acumulated=0
    for grade in self.grades:
      acumulated =+grade
    return acumulated/self.grades

  def print_grades(self):
    for grade in self.grades:
      print(grade.score)
    return
  def set_attendance(self,date, present):
    self.attendance['date']=present


class Grade():
  minimum_passing = 65
  def __init__(self, score):
    self.score = score
  def is_passing(self, score):
    if self.score >= Grade.minimum_passing:
      return True
    else:
      return False


roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)

pieter.add_grade(Grade(100))
pieter.add_grade(Grade(80))

pieter.print_grades()
pieter.attendance[datetime.datetime.now().strftime("%j")]=present

i get this error in console:

Traceback (most recent call last):
  File "script.py", line 44, in <module>
    pieter.attendance[datetime.datetime.now().strftime("%j")]=present
NameError: name 'present' is not defined

I think is a problem with the date format not recognized. Or maybe i didn’t import correctly the module. Or i am trying to access the dictionary by a key is not created yet…

I am trying to figure out those two issues:

  • Get the list of scores for pieter instead of memory address (i saw a perfect approach above given by
  • And how to create the keys of dictionary attendance based on actual date when called

Really great! This is what i was looking for to return the scores inside the list for each instance. Thx so much! :smiley: I getting now the difference with the other solutions provided.

then you could implement __str__ or __repr__ method in the grade class. Have you worked with these methods before? You can consult documentation/tutorial on these methods if you have to

what should the dictionary look like? a date as key, and a boolean for value? when someone was present or not?

like the error says, present is undefined/unnamed variable

is removing the wrapper really the way to go?

In this solution you are printing no the values inside a list but you are printing the values each one in each row, isn’t it? Thx anyway. I think have the head full of videogames and i am not paying proper attention. Sorry for that haha

  • Yes. I worked previously over this in previou part of this class block. I will try a solution with that. I saw there is some better approaches like using a comprehensive list or use the method to print? Am i correct? :sweat_smile:
  • I am trying to figure out the differences here and why some approaches are better than others. Especially when the moderators said better don’t manipulate the object itself to access the data :sweat_smile:

-Yes. This is the way…i am looking for. i had a mess. I confused present for something with date instead of present of presence. I will work on that.

Im struggling to understand what is the answer to this question because, being honest, i don’t know the difference in approaches yet. What you say is, what is the sense for accesing one data inside an object to remove the wrapper of that data instead of looking ways to find that data without removing this ‘wrapper’. Isn’t it?

I know are many questions and really thanks xd This concept of class methods and instance methods is really important for me, i saw it 12131230 times over the internet but this time i am working on that in practical way. I have the impression i just learn solving the own errors… cuz when you see many tutorials everything matches what they said but after you have to solve the puzzles by yourself xdd

depends on your definition of better. But making a method for printing would have my personal preference I think

manipulating the access is the way to go, manipulating the data means changes to your program/code.

but then you should simply have a boolean:

pieter.attendance[datetime.datetime.now().strftime("%j")]=true

Grade is a wrapper here, but this again boils down to the same discussion about manipulating object vs access to the data

1 Like

Thanks sooo much for the whole clarifications. I have a path to follow now :slight_smile:

  • I am figuring out better now about this score access

  • I have made some manipulations and the attendance method is working. Definitely, i was giving the value of the variable present but the variable present was never settled up any value but was required in the method as parameter and that’s why unassigned when you called it without any prior value as i understand.

Thanks for all the hints! I see now :grin:

1 Like