Question: I am having trouble with an hw problem in my CS class. The problem has to do with creating a class in python. Heres the prompt

Your class should be named Student, and you should define the following methods:

`__init__`: This method initializes a Student object.

Parameters: a name, a GPA, and a number of units taken

• Should initialize instance variables for name, GPA, and units based on
the information that was passed in. If the GPA or number of units is negative, sets it to 0. (Donʼt worry about non-numeric values in this method.)

update: This method updates the instance variables of the Student object if the Student takes a new class.

• Parameters: units for the new class, grade points earned (as a number) in the new class.

• Should modify the instance variable for units to add the units for the new class

• Should modify the GPA to incorporate the grade earned in the new class. (Note that this will be a weighted average using both the unit counts and both sets of GPAs.)

get\_gpa: This method should return the studentʼs GPA. 

get\_name: This method should return the studentʼs name.

**Heres what i have**

```
class Student:
  def__init__(self,name,GPA,units):
    if units <0:
      units=0
    if GPA<0:
      GPA=0
    self.name=name
    self.GPA=GPA
    self.units=units

  def update(newunits,GPE):

```

thats all i can come up with

Answer: 
Let’s go through some points which will hopefully help you:

### Constructor (`__init__`)

> 
> If the GPA or number of units is negative, sets it to 0. 
> 
> 
> 

So you probably want to check each separately:

```
if units < 0:
    units = 0
if GPA < 0:
    GPA = 0

```

### Update method

Methods in general take a reference to the current object as the first argument, named `self` per convention (just as in `__init__`). So your update method declaration should look like this:

```
def update(self, newunits, GPE):
    ...

```

> 
> Should modify the instance variable for units to add the units for the new class
> 
> 
> 

Just as you did in the constructor, you can access instance variables using `self.varname`. So you probably want to do something like this:

```
self.units += newunits

```

> 
> Should modify the GPA to incorporate the grade earned in the new class. (Note that this will be a weighted average using both the unit counts and both sets of GPAs.)
> 
> 
> 

Just as you update `self.units` you have to update `self.GPA` here. Unfortunately, I have no idea what a GPA is and how it is calculated, so I can only guess:

```
self.GPA = ((self.GPA * oldunits) + (GPE * newunits)) / self.units

```

Note that I introduced a new local variable `oldunits` here that simply stores the units temporarily from *before* it was updated (so `oldunits = self.units - newunits` after updating).

### get\_gpa and get\_name

These are simple getters that just return a value from the object. Here you have an example for the **units**, i.e. you should figure it out for the actual wanted values yourself:

```
def get_units (self):
    return self.units

```

Note that it’s rather unpythonic to have getters (`get_x` methods), as you would just expect people to access the properties directly (while handling them with care).
I'll help you to complete the question, but let me point out a little mistake first:

```
if units <0 and GPA <0:
  units=0
  GPA=0

```

This will set units and GPA to zero only if they are both negative. You'll want to set each to zero if it's negative, even if the other is not. Change it to:

```
if units < 0: units = 0
if GPA < 0: GPA = 0

```

Concerning your update method, the correct signature would be:

```
def update(self, newunits, GPE):

```

Python object methods should always start with self.

Now, I'm not sure about how to calculate the GPA (we use a different system were I live), but according to some quite google queries, your update method should be something like:

```
def update(self, newunits, GPE):
    GPE_earned_before = self.GPA * self.units
    total_GPE = GPE_earned_before + GPE
    self.GPA = total_GPE / (self.units + newunits)
    self.units += newunits

```

I used a lot of variables here, to make things clear, but this can be shortened to:

```
def update(self, newunits, GPE):
    self.GPA = (self.GPA * self.units + GPE) / (self.units + newunits)
    self.units += newunits

```