Why the extra 'None'?


#1



https://www.codecademy.com/courses/python-intermediate-en-egNXj/1/2?curriculum_id=4f89dab3d788890003000096


Hello

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?

new
None
used
None


I would expect to see:

new
used
None


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


#2

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


#3

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?


#4

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
my_car.drive_car()

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)


#5

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