This Looks Like a Job For... Help please!


#1


This code gave me a pass @appylpye I feel there is something still wrong
question

1) I thought in the method (self ) always needs to be there but I add it gives me error reading
Oops, try again. milton.full_time_wage(0) caused the following exception: calculate_wage() takes exactly 2 arguments (3 given)

however, when entered as below it gives me a pass!

2) I never saw page printing 20$ per hour as mentioned in the instruction, why does it still work tho?

def full_time_wage(self, hours):
        self.hours = hours
        return super(PartTimeEmployee, self).calculate_wage(hours)

    """Models real-life employees!"""
    def __init__(self, employee_name):
        self.employee_name = employee_name

    def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00
class PartTimeEmployee(Employee):
    def calculate_wage(self, hours):
        self.hours = hours
        return hours * 12  
    def full_time_wage(self, hours):
        self.hours = hours
        return super(PartTimeEmployee, self).calculate_wage(hours)
        
milton = PartTimeEmployee("employee")
print milton

PartTimeEmployee.calculate_wage
Employee.calculate_wage

however, when it gives a pass, it also prints this in console

<PartTimeEmployee object at 0x7f10a6b1ce10>
None


#2

@orouge,

Part of your code is not posted, so we cannot test what is there. In addition, some of the indention of your posted code is incorrect. For example, you have ...

    def calculate_wage(self, hours):
    self.hours = hours
    return hours * 20.00

In any case, the reason why you saw something similar to this in the output, which includes a memory address ...

<PartTimeEmployee object at 0xbf2b50>

... is that you have this line, which outputs a default string representation of the milton object ...

print milton

The exercise is actually asking you, instead, to output the result of calling milton's full_time_wage method, with 10 hours of work ...

print milton.full_time_wage(10)

#3

yes I can see my mistake now. thanks for point to it. Indentation was by mistake too.

I have one more question. Also @appylpye sorry for shooting all my questions at you :wink:)) it is because I find your explanations the most conducive to my understanding.

This code passed! Also it is ridiculous that it is giving me a pass considering in the method I don't have args --angle1-2-3 but below i assign them with self. Why is it even giving me a pass?

class Triangle(object):
    def __init__(self):  # method
        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3
    number_of_sides = 3
    def check_angles(self):
        if self.angle1+self.angle2+self.angle3  == 180:
            return True 
        else:
            return False

However, when i entered the following it didn't even though it is basically the same thing. do you know why?

added to the code(under check_angles)
self.angle1 = angle1
self.angle = angle2
self.angle = angle3
I know that instruction reads add up the args but even if I have extra code as above it should work. No?

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):  # method
        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3
    number_of_sides = 3
    def check_angles(self):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        if self.angle1 +  self.angle2 + angle3 == 180:
            return True 
        else:
            return False

I think the following would be correct one but it gives me an error . the ones without args only with self passes?!

class Triangle(object):
    def __init__(self, angle1, angle2, angle3):  # method
        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3
    number_of_sides = 3
    def check_angles(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        if self.angle1 +  self.angle2 + angle3 == 180:
            return True 
        else:
            return False

#4

Hi @orouge ,

This version of the check_angles method is correct, with self as the only parameter ...

    def check_angles(self):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        if self.angle1 +  self.angle2 + angle3 == 180:
            return True 
        else:
            return False

... because the only information the method needs to be given to do its work is a reference to the instance of Triangle on which it is to perform that work. Once given that reference, the method can find the values of angle1, angle2, and angle3 for that instance, and then perform the calculation. Those values were assigned when the instance was first created, and __init__ was called. The information is stored in memory as part of the instance, so there is no reason to assign it again or to pass it through parameters to the check_angles method.

So, after the class and its methods have been defined, you can do the following ...

# create an instance of Triangle named my_triangle
my_triangle = Triangle(90,30,60)
# print its number_of_sides
print my_triangle.number_of_sides
# print a result of calling its check_angles method
print my_triangle.check_angles()

When this line is executed ...

print my_triangle.check_angles()

... a reference to the my_triangle instance is passed as a reference, via the check_angles method's self parameter. Then, within that check_angles method, any information that is needed about that instance is available through the name, self.


#5

@appylpye i am at "Instantiate an Object" following instruction step by step and it gives the following error still. in the instance I have to give 3 args as instruction says so however adding self to it as arg in the init makes it four. and that is when error occurs. but i can't eliminate it as it is in the instruction

Traceback (most recent call last):
  File "python", line 15, in <module>
TypeError: __init__() takes exactly 1 argument (4 given)

class Triangle(object):
    def __init__(self):  # method
        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3
    number_of_sides = 3
    def check_angles(self):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
        if self.angle1 +  self.angle2 + angle3 == 180:
            return True 
        else:
            return False
my_triangle = Triangle(90, 30, 60) #instance
print my_triangle.number_of_sides
print my_triangle.check_angles()

#6

Hi, @orouge ,

To initialize a Triangle object, we need to assign three sides, so we need parameters for self, angle1, angle2, and angle3. But you only have a parameter for self in the method header, here ...

def __init__(self):  # method

Assuming that the __init__ header has been corrected, the first of the following three statements is correct, but in the second and third, you are assigning values to self.angle, instead of to the appropriate instance variables ...

        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3

#7

@appylpye i am sorry i still didn't get it right after I added sides as args then adding them up by creating instances nothing seems to be working. Can you pleas explain on the correct code. I am a visual learner and learn better looking at data in comparison. Thanks again!


#8

See the following commented code ...

class Triangle(object):
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    
    def check_angles(self):
        return self.angle1 + self.angle2 + self.angle3 == 180

# Create a Triangle instance, and assign it to my_triangle.
my_triangle = Triangle(90, 30, 60)
# Output the number of sides.
print my_triangle.number_of_sides
# Output the result of a call to check_angles().
# We do not pass it an explicit argument, because my_triangle,
# the object from which we call the method,
# becomes automatically linked to the first
# (and in this case, only), parameter,
# which we have named self, as is customary.
print my_triangle.check_angles()

#9

@appylpye I am a bit confused. lesson gave me this previous code

class Triangle(object):
    def __init__(self):  # method
        self.angle1 = angle1
        self.angle = angle2
        self.angle = angle3
    number_of_sides = 3
    def check_angles(self):
        if self.angle1 +  self.angle2 + angle3 == 180:
            return True 
        else:
            return False
``` 
 and according to instruction I added the following. Bit it gave this error

```
Traceback (most recent call last):
  File "python", line 12, in <module>
TypeError: __init__() takes exactly 1 argument (4 given)
```
```
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_tirangle.check_angles()
```



Then I added your sent code  with my_triangle part--to try to see the difference but it still gave error. the part that is about Creating instance and then printing sides and angles separately I understand. 
```
class Triangle(object):
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    
    def check_angles(self):
        return self.angle1 + self.angle2 + self.angle3 == 180 # also isn't it syntax error if we return and equalize??
        
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angle()

I spent so much on this but still can't figure it out yet. The comments you sent i entered initially too, I think I am entering something wrong before creating instance but can't figure out. I would appreciate if you could send me whole correct code so I can study it on my own. if anything i will ask afterwards


#10

The code posted in my previous reply passes when submitted for Exercise 17: Instantiate an Object, and is appropriate for study.


#11

Along with what Glenn gave you to study, the above error message really does spell out the issue.


#12

I kinda wanted to fix that issue too, @mtf I added the argument or left it alone with only self but still didn't work. I wonder if this code can be kept too while fixing argument issue?

class Triangle(object):
    number_of_sides = 3
    def __init__(self, angle1, angle2, angle3):
        self.angle1 = angle1
        self.angle2 = angle2
        self.angle3 = angle3
    
    def check_angles(self):
        return self.angle1 + self.angle2 + self.angle3 == 180 # also isn't it syntax error if we return and equalize??
        
my_triangle = Triangle(90, 30, 60)
print my_triangle.number_of_sides
print my_triangle.check_angle()

#13

This is not an assignment, but a comparison that yields a boolean, which is a value.


#14

Compare the method name in the header ...

def check_angles(self):

... to the method name in the call here ...

print my_triangle.check_angle()

#15

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.