Python,向类添加方法,错误:未定义全局名称

2024-10-03 15:25:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图理解为什么我的方法increment4不起作用。所有这些方法作为函数都可以很好地工作,但现在我已经将它们转换为方法,有些方法不起作用。我已将“Time”对象名替换为“self”。我试着保留和删除“return”。 编辑我没有使用点符号,正如有人善意地指出的那样。我做了一些改变。现在Python给了我一个不同的错误:

Traceback (most recent call last):
  File "/Users//Desktop/temp.py", line 33, in <module>
    currenttime.increment4(4000)
  File "/Users//Desktop/temp.py", line 22, in increment4
    newtime = float(total_seconds).make_time()
AttributeError: 'float' object has no attribute 'make_time'
>>> 

第33行是:

currenttime.increment4(4000)

第22行是:

newtime = float(total_seconds).make_time()

事情是这样的:

class Time:
    def printTime(self):
        print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds)

    def make_time (self):
        time = Time ()
        time.hours = self / 3600
        time.minutes = (self % 3600) / 60
        time.seconds = (self % 3600) % 60
        return time

    def covertTOseconds (self):
        hours = self.hours * 3600
        minutes = self.minutes * 60
        seconds = self.seconds
        amt_in_seconds = hours + minutes + seconds
        return amt_in_seconds

    def increment4 (self, increaseINseconds):
        total_seconds = self.covertTOseconds() + increaseINseconds
        newtime = float(total_seconds).make_time()
        newtime.printTime()


currenttime = Time()
currenttime.hours = 3
currenttime.minutes = 47
currenttime.seconds = 45



currenttime.increment4(4000)

Tags: 方法inselfmaketimedeffloattotal
1条回答
网友
1楼 · 发布于 2024-10-03 15:25:35

您需要稍微修改一下代码:

class Time:
    def printTime(self, time):
        print str(time.hours)+":"+str(time.minutes)+":"+str(time.seconds)

    def increment4(self, increaseINseconds):
        seconds = self.covertTOseconds() + increaseINseconds
        time = self.makeTime (seconds)
        print time

    def makeTime(self, totalseconds):
        time = Time ()
        time.hours = totalseconds / 3600
        time.minutes = (totalseconds % 3600) / 60
        time.seconds = (totalseconds % 3600) % 60
        return time

    def covertTOseconds(self):
        hours = self.hours * 3600
        minutes = self.minutes * 60
        seconds = self.seconds
        totalseconds = hours + minutes + seconds
        return totalseconds

如错误所示,converTOseconds没有定义,因为它应该用self调用,而且它也不接受参数。因此,您需要从covertTOseconds (self)更改为self.convertTOseconds()。希望这有帮助。你知道吗

既然你已经更新了这个问题,在我看来,你可能想做的是有这样一个类:

class Time:
    def __init__(self):
        self.hours = None
        self.minutes = None
        self.seconds = None
        self.total_seconds = None

    def to_seconds(self):
        hours = self.hours * 3600
        return (self.hours * 3600) + (self.minutes * 60) + self.seconds

    def increment(self, x):
        self.total_seconds = self.to_seconds() + x
        self.hours = self.total_seconds / 3600
        self.minutes = (self.total_seconds % 3600) / 60
        self.seconds = (self.total_seconds % 3600) % 60
        self.__str__()

    def __str__(self):
        print("{0}:{1}:{2}".format(self.hours, self.minutes, self.seconds))

然后可以按如下方式使用该类:

c_time = Time()
c_time.hours = 3
c_time.minutes = 47
c_time.seconds = 45

c_time.increment(4000)

这将输出:

4:54:25

相关问题 更多 >