TypeError: unsupported operand type(s) for +: 'int' and 'str' for python

TypeError: unsupported operand type(s) for +: 'int' and 'str' for python

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()

所以我试图为时间制作一个 class 并且有它的功能等等,但我还试图创建 2 个时间实例,一个是默认的,一个是用户定义的。我认为这是正确的,但我一直收到此错误。任何帮助

问题出在这里:

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

您使用运算符加号 (+) ":" <-- str + self.get_seconds() <-- int

连接 str 类型和 int 类型

所以你必须写:

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)

这就是为什么它说:不支持类型的操作数

您似乎不熟悉如何将数据 concatenate/interpolate 转换为字符串。在 Python 中,您不能使用 + 来连接字符串,除非两个操作数都是字符串。此外,连接在 Python 中并不是真正的惯用语,人们通常更喜欢 interpolation/formatting.

在这种情况下你有两个选择:

  1. 使用字符串插值:

    def print_time(self):
        print("{0}:{1}:{2}".format(self.get_hour(), self.get_minutes(), self.get_seconds()))
    
  2. 使用字符串.join方法:

    def print_time(self):
        print(':'.join(self.get_hour(), self.get_minutes(), self.get_seconds()))
    

回溯告诉你你需要知道的一切。您正在尝试将字符串添加到 int。发生的最明显的地方在这里:

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())