bw61293
bw61293

Reputation: 91

Python classes and methods

I have online practice questions for python programming. I am stuck on this question:

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. 

I have this answer, but I do not know what the problem is. It keeps telling me I have an incorrect value somewhere.

class WeatherForecast (object):
    def __init__ (self):
        self.skies = ''
        self.high = ''
        self.low  = ''
    def set_skies (self, value):
        self.skies = value
    def set_high (self, value):
        self.high = value
    def set_low (self):
        self.low = low
    def get_skies (self):
        return self.skies
    def get_high(self):
        return self.high
    def get_low(self):
        return self.low

Upvotes: 2

Views: 1189

Answers (4)

joshua riddle
joshua riddle

Reputation: 21

The problem is most likely that high and low are supposed to be integers.

class WeatherForecast:
    def __init__(self):
        self.__skies=""
        self.__high=0
        self.__low=0

    def set_skies(self, skies):
        self.__skies=skies

    def set_high(self,high):
        self.__high=high

    def set_low(self,low):
        self.__low=low

    def get_skies(self):
        return self.__skies

    def get_high(self):
        return self.__high

    def get_low(self):
        return self.__low

Upvotes: 1

Zoltan S
Zoltan S

Reputation: 21

I believe you are missing an argument in set_low.

def set_low (self, value):
    self.low = value

Upvotes: 0

Jian Cui
Jian Cui

Reputation: 825

For example, in this method:

def set_high(self, value):    
    self.high = high

You are trying to assign high to self.high, but you didn't say where high come from, the program only have variable named value. So you should change like this:

def set_high(self, high):
    self.high = high

Upvotes: 0

anon582847382
anon582847382

Reputation: 20391

You don't ever define skies, high or low.

Perhaps in the functions where you set things, you mean:

def set_high (self, value):  # Do this for all setting functions.
    self.high = value  # value is defined, so will work.

Upvotes: 5

Related Questions