gpt4 book ai didi

Python 类和方法

转载 作者:行者123 更新时间:2023-11-28 21:20:13 25 4
gpt4 key购买 nike

我有 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

最佳答案

您永远不会定义天空highlow

也许在您设置的函数中,您的意思是:

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

关于Python 类和方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23093492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com