Why the extra 'None'?




I do not understand why I get the extra 'None' between the 'new' and 'used in the '8. Modifying member variables' lesson. Can anyone explain, please?


I would expect to see:


class Car(object):
    condition = "new"
    def __init__(self, model, color, mpg):
        self.model = model
        self.color = color
        self.mpg   = mpg
    def display_car(self):
        print "This is a " + self.color + " " + self.model + \
                " with " + str(self.mpg) + " MPG."
    def drive_car(self):
        self.condition = "used"

my_car = Car("DeLorean", "silver", 88)
print my_car.condition
print my_car.drive_car()
print my_car.condition


by default function and methods return None, so drive_car returns None, because you add a print keyword when calling drive_car, None will be printed


Thanks for the reply, stetim94. A couple more questions, if that's okay:

1) As 'drive_car' returns 'None' because there has been no 'return' in the function, is there any way of preventing the 'None' from displaying?

2) Is the last 'None' that always displayed due to the programme ending? Is the 'script.py' effectively seen as another function?


not print what the function returns:

# prints the returned result of drive_car method
print my_car.drive_car()
# calls the function, the method will print
# we don't need to print what the method returns

i think this is caused by codecademy, run your code here, this gives you the option to run python code where there is no SCT (submission correctness test, what codecademy uses to validate your code)


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