TypeError:+;的操作数类型不受支持:';int';和';str';对于python

TypeError:+;的操作数类型不受支持:';int';和';str';对于python,python,class,types,multiple-instances,Python,Class,Types,Multiple Instances,所以我试图为时间创建一个类,还有它的函数等等,但我也试图创建两个时间实例,一个是defult,一个是用户定义的。我认为这是正确的,但我一直得到这个错误。任何帮助问题在这里: class Time: """The time class defines the time with the following attributes: hour ,minutes , second """ def __init__(self,hour=12,minutes=0,second

所以我试图为时间创建一个类,还有它的函数等等,但我也试图创建两个时间实例,一个是defult,一个是用户定义的。我认为这是正确的,但我一直得到这个错误。任何帮助

问题在这里:

class Time:
    """The time class defines the time with the following attributes:
    hour ,minutes , second
    """
    def __init__(self,hour=12,minutes=0,seconds=0):
        self.hour = hour
        self.minutes = minutes
        self.seconds = seconds

    #attributes
    hour = 12
    minutes = 0
    seconds = 0

    def get_hour(self):
        return self.hour

    def get_minutes(self):
        return self.minutes

    def get_seconds(self):
        return self.seconds

    def print_time(self):
        print(self.get_hour()+":"+self.get_minutes()+":"+self.get_seconds())

    def set_hour(self, new_hour):
        self.hour = new_hour

    def set_minutes(self, new_minutes):
        self.minutes = new_minutes

    def set_seconds(self, new_seconds):
        self.seconds = new_seconds

    def increment_seconds(self):
        if self.seconds == 59:
            self.seconds = 0
            self.minutes = self.minutes + 1
        else:
            self.seconds = self.seconds + 1
    def increment_minutes(self):
        if self.minutes == 59:
            self.minutes = 0
            self.hour = self.hour + 1
        else:
            self.minutes = self.minutes + 1
    def increment_hour(self):
        if self.hour == 12:
            self.hour = 1
        else:
            self.hour = self.hour + 1

print("making 2 instances of time")
time1 = Time()
time2 = Time(14,34,12)
print("this is before")
print("normal time",time1.print_time())
print("user time",time2.print_time())

time1.increment_hour()
time1.increment_minutes()
time1.increment_seconds()
print("after")
time1.print_time()


time2.increment_hour()
time2.increment_minutes()
time2.increment_seconds()
print("after")
time2.print_time()

使用运算符plus(+)“:”连接str-type和int-type,您似乎不熟悉如何将数据连接/插入到字符串中。在Python中,除非两个操作数都是字符串,否则不能使用
+
连接字符串。此外,连接在Python中并不是真正惯用的,人们通常更喜欢插值/格式化

在这种情况下,您有两种选择:

  • 使用字符串插值:

    print(str(self.get_hour())+":"+str(self.get_minutes())+":"+str(self.get_seconds()))
    
    a = 3
    b = "test"
    
    print a + b <-- this is not working (str + int)
    print str(a) + b <-- this is working (str + str)
    
  • 使用字符串
    .join
    方法:

    def print_time(self):
        print("{0}:{1}:{2}".format(self.get_hour(), self.get_minutes(), self.get_seconds()))
    

  • 追踪告诉你你需要知道的一切。您正在尝试向int添加字符串。最明显的情况是:

    def print_time(self):
        print(':'.join(self.get_hour(), self.get_minutes(), self.get_seconds()))
    
    这里有一个可能的解决方案:

    def print_time(self):
            print(self.get_hour()+":"+self.get_minutes()+":"+self.get_seconds())
    

    正如错误所说,您不能添加字符串和整数。您能提供更多信息吗?哪一行导致错误?它在打印时间函数中。我已经回答了他的问题,也许他会接受。
    def print_time(self):
            print(self.get_hour()+":"+self.get_minutes()+":"+self.get_seconds())
    
    def print_time(self):
            print('{}:{}:{}'.format(self.get_hour(), self.get_minutes(), self.get_seconds())