Python类和方法
我有一些在线的Python编程练习题。现在我卡在了一个问题上:
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.
我有这个答案,但我不知道问题出在哪里。系统一直告诉我某个地方的值不正确。
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
4 个回答
0
我觉得你在 set_low 这个函数里缺少了一个参数。
def set_low (self, value):
self.low = value
0
比如,在这个方法里:
def set_high(self, value):
self.high = high
你试图把high的值赋给self.high,但是你没有说明high是从哪里来的,程序里只有一个叫value的变量。所以你应该这样修改:
def set_high(self, high):
self.high = high
1
这个问题很可能是因为高和低应该是整数。
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
5
你从来没有定义过 skies
、high
或 low
这些东西。
也许在你设置这些东西的函数里,你的意思是:
def set_high (self, value): # Do this for all setting functions.
self.high = value # value is defined, so will work.