Python Help: Write the definition of a class WeatherForecast -
write definition of class weatherforecast provides following behavior (methods):
a method called set_skies has 1 parameter, string. method called set_high has 1 parameter, int. method called set_low has 1 parameter, int. method called get_skies has no parameters , returns value last used argument in set_skies . method called get_high has no parameters , returns value last used argument in set_high . method called get_low has no parameters , returns value last used argument in set_low .
no constructor need defined. sure define instance variables needed "get"/"set" methods.
class weatherforecast(object): def __init__ (self, skies, value): self.skies = "" value = 0 def get_skies(): return self.set_skies def set_skies(self, value) self.skies = value def get_high(): return self.set_high def set_high(self, value): self.high = value def get_low(): return self.set_low def set_low(self, value): self.low = value class weatherforecast(): skies = "clear" high = 80 low = 20 def set_skies(self, skies) self.skies = skies def get_skies(self): return self.skies def set_high(self, high): self.high = high def get_high(self): return self.high def set_low(self, value): self.low = value def get_low(self): return self.low
it seems though missing instance variables class.
the spec requires have 1 string describing skies (ie cloudy, sunny) , 2 integers represent low , high temperatures day (ie low of 23 , high of 45)
the spec says not have deal them in constructor, yet may if wish.
since homework assignment assume, put on right course; don't want give answer.
variables:
- string skies
- int low temp
- int high temp
functions:
get/set skies. should return self.skies, set should set instance variable skies.
get/set low temp. should return self.low, set should set instance variable low temp.
get/set high temp. should return self.high, set should set instance variable high temp.
good luck assignment.
if feel looking syntax here link python classes. https://docs.python.org/2/tutorial/classes.html
edit: did not give op code, wrote (minus 1 missing colon)
class weatherforecast(): skies = "clear" high = 80 low = 20 def set_skies(self, skies): self.skies = skies def get_skies(self): return self.skies def set_high(self, high): self.high = high def get_high(self): return self.high def set_low(self, value): self.low = value def get_low(self): return self.low
you run code making object of class type , running these class methods on it.
ie)
w = weatherforecast() w.set_skies("clear") w.set_low(20) w.set_high(30) w.get_skies() w.get_low() w.get_high()
Comments
Post a Comment