小编mat*_*s25的帖子

在类 Python 中调用方法

class Time:

    def __init__(self,x,y,z):
        self.hour=x
        self.minute=y
        self.second=z

    def __str__(self):
        return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

    def time_to_int(time):
        minutes=time.hour*60+time.minute
        seconds=minutes*60+time.second
        return seconds

    def int_to_time(seconds):
        time=Time()
        minutes,time.second=divmod(seconds,60)
        time.hour,time.minute=divmod(minutes,60)
        return time

    def add_time(t1,t2):
        seconds=time_to_int(t1)+time_to_int(t2)
        return int_to_time(seconds)

start=Time(9,45,00)
running=Time(1,35,00)
done=add_time(start,running)
print(done)
Run Code Online (Sandbox Code Playgroud)

我是 python 的新手,最近我一直在做一些练习。我遇到了一个问题,我已经编写了相同的代码。但我反复收到错误:“add_time 未定义”。我尝试定义一个 main() 方法,但它没有打印任何内容。请帮忙。

python methods class

8
推荐指数
1
解决办法
3万
查看次数

标签 统计

class ×1

methods ×1

python ×1