Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialsandeep Singh
381 Pointsstill not working
even if I declare laps = 0 globally I did the way you told but didn't work then I even declared 'length' locally whats missing with some coding instructions and please a bit explanation of your logic
class RaceCar:
def __init__(self, color, fuel_remaining, *args, **kwargs):
self.color = color
self.fuel_remaining = fuel_remaining
self.laps = 0
for key,value in kwargs.items():
setattr(self, key, value)
def run_lap(self, length):
self.length = length
self.fuel_remaining -= (length*0.125)
laps += 1
3 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou're very close! laps += 1
should be self.laps += 1
Hara Gopal K
Courses Plus Student 10,027 Pointsi guess its not required to add
self.length = length
may be because length is already an instance attribute, we don't need the self again...
Chris Freeman
Treehouse Moderator 68,441 PointsCorrect. self.length
is not required. It's purpose would have been to hold onto the last value of length passed in to run_lap
by binding it to this instance of RaceCar
.
length
itself is not an attribute. It is only a parameter value used as a temporary local variable that is thrown away after the run_lap
method is completed.
Hara Gopal K
Courses Plus Student 10,027 Pointsthanks chris. yes length is a parameter. just learned that... so can we call the parameters in the init as attributes, since we are assigning properties to them ?
Chris Freeman
Treehouse Moderator 68,441 PointsWhile the terms parameter, argument, and attribute get used loosely sometimes, there are more precise meanings for these terms.
From the glossary:
A value passed to a function (or method) when calling the function. There are two kinds of argument:
keyword argument: an argument preceded by an identifier (e.g. name=) in a function call or passed as a value in a dictionary preceded by **. For example, 3 and 5 are both keyword arguments in the following calls to complex():
complex(real=3, imag=5) complex(**{'real': 3, 'imag': 5}) positional argument: an argument that is not a keyword argument. Positional arguments can appear at the beginning of an argument list and/or be passed as elements of an iterable preceded by *. For example, 3 and 5 are both positional arguments in the following calls:
complex(3, 5) complex(*(3, 5)) Arguments are assigned to the named local variables in a function body. See the Calls section for the rules governing this assignment. Syntactically, any expression can be used to represent an argument; the evaluated value is assigned to the local variable.
See also the parameter glossary entry, the FAQ question on the difference between arguments and parameters, and PEP 362.
A value associated with an object which is referenced by name using dotted expressions. For example, if an object o has an attribute a it would be referenced as o.a.
A named entity in a function (or method) definition that specifies an argument (or in some cases, arguments) that the function can accept. There are five kinds of parameter:
positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument. This is the default kind of parameter, for example foo and bar in the following:
def func(foo, bar=None): ... positional-only: specifies an argument that can be supplied only by position. Python has no syntax for defining positional-only parameters. However, some built-in functions have positional-only parameters (e.g. abs()).
keyword-only: specifies an argument that can be supplied only by keyword. Keyword-only parameters can be defined by including a single var-positional parameter or bare * in the parameter list of the function definition before them, for example kw_only1 and kw_only2 in the following:
def func(arg, *, kw_only1, kw_only2): ... var-positional: specifies that an arbitrary sequence of positional arguments can be provided (in addition to any positional arguments already accepted by other parameters). Such a parameter can be defined by prepending the parameter name with *, for example args in the following:
def func(*args, **kwargs): ... var-keyword: specifies that arbitrarily many keyword arguments can be provided (in addition to any keyword arguments already accepted by other parameters). Such a parameter can be defined by prepending the parameter name with **, for example kwargs in the example above.
Parameters can specify both optional and required arguments, as well as default values for some optional arguments.
See also the argument glossary entry, the FAQ question on the difference between arguments and parameters, the inspect.Parameter class, the Function definitions section, and PEP 362.