[Tutor] Not sure what I'm doing wrong with these 2 python scripts
Steven D'Aprano
steve at pearwood.info
Wed Nov 20 11:30:17 CET 2013
On Tue, Nov 19, 2013 at 06:22:32PM -0600, Anton Gilb wrote:
> Not sure what I'm doing wrong, here are the problems and what I have for
> answers so far.
What makes you think you're doing anything wrong? See below for more
comments.
> 1.)Write the definition of a class Counter containing:
> An instance variable named counter of type int .
> A constructor that takes one int argument and assigns its value to counter
> A method named increment that adds one to counter . It does not take
> parameters or return a value.
> A method named decrement that subtracts one from counter . It also does
> not take parameters or return a value.
> A method named get_value that returns the value of the instance variable
> counter .
>
> class Counter(object):
> def __init__(self, ct):
> self.counter = ct
> def increment(self):
> self.counter += 1
> def decrement(self):
> self.counter -= 1
> def get_value(self):
> return self.counter
This looks fine to me, except for a couple of little nit-picks about
terminology. (If you care about them, feel free to ask, otherwise I'll
just bite my tongue.) What errors are you getting? Nothing is obvious.
> 2.)Write the definition of a class WeatherForecast that provides the
> following behavior (methods):
> A method called set_skies that has one parameter, a String.
> A method called set_high that has one parameter, an int.
> A method called set_low that has one parameter, an int.
> A method called get_skies that has no parameters and that returns the
> value that was last used as an argument in set_skies .
> A method called get_high that has no parameters and that returns the value
> that was last used as an argument in set_high .
> A method called get_low that has no parameters and that returns the value
> that was last used as an argument in set_low .
> No constructor need be defined. Be sure to define instance variables as
> needed by your "get"/"set" methods.
Again, what errors are you getting?
In this case, I can guess what some of the errors might be. See below.
> class WeatherForecast(object):
> def __init__ (self):
> self.skies = ""
The instructions say that you don't have to define a constructor, but
they don't forbid it. In this case, I think I'd prefer not to include
the constructor.
(I know I said I wouldn't, but here's one of the nit-picks: __init__
isn't strictly speaking a constructor, it's an initialiser. Not that the
difference really matters here.)
> def get_skies():
> return self.set_skies
Here, rather than return the skies value, you return the "set_skies"
method itself. I suggest you look at the first class and consider how
the get_value method works. You want to return the "skies" value
instead.
> def set_skies(self, value)
> self.skies = value
> def get_high():
> return self.set_high
Likewise, here rather than return the high value, you return the
"set_high" method.
> def set_high(self, value):
> self.high = value
> def get_low():
> return self.set_low
And likewise again.
> def set_low(self):
> self.low = value
Hope this helps,
--
Steven
More information about the Tutor
mailing list