我有2个整数值,其中包含'-',想将其拆分..但获取错误int不包含split属性[关闭]

def splitIn(x):
   t = x. Split('-')
   return (t[0] + t[1])/2

splitIn(7-8)

我有2个整数值,其中包含'-',想将其拆分..但获取错误int不包含split属性

评论
  • 懒喵喵~
    懒喵喵~ 回复

    You have to convert your numbers to String first. What you are doing is essentially passing -1 to the function.

    splitIn('7-8')
    
  • yeum
    yeum 回复

    You are mixing up some types like no quotes around the 7-8 and split() returns a string not an int so you can't add them. Try this code:

    def splitIn(x):
       t = x.split('-')
       return (int(t[0]) + int(t[1]))/2
    
    print(splitIn('7-8'))
    
  • Oswal
    Oswal 回复

    Split是类类型string的内置函数。由于您要传递整数类型的对象,因此您显然会得到错误。

    选项1:-

    def splitIn(x): 
        t = str(x).split('-') 
        return (t[0] + t[1])
     splitIn(7-8)