2016-06-10 85 views
-1
class Solution(): 
    def isHappy(self,n): 
     t = n 
     z = n  
     while t>0: 
      t = self.cal(t) 
      if t == 1: 
       return True 
      z = self.cal(self.cal(z)) 
      if z == 1: 
       return True 
      if t == z: 
       return False 

    def cal(self,n): 
     x = n 
     y = 0 
     while x > 0: # unorderable types: NoneType() > int() 
      y = y+(x%10)*(x%10) 
      x = x/10 



test = Solution() 
result = test.isHappy(47) 
print(result) 

我在得到错误消息 “而X> 0”, “unorderable类型:NoneType()> INT()”。我将其更改为“while int(x)> 0”,但其他错误消息 “int()参数必须是字符串,类似字节的对象或数字,而不是 'NoneType'”。任何帮助,感谢您的时间。非常感谢!的Python unorderable类型:NoneType()> int()函数

回答

2

的问题是不言自明的:的n传递给cal()值变为None,这是不能进行比较。确保在cal()方法结束时返回适当的值,这就是None的来源。加入这样的事情在cal()年底:

return x # or `y`, depending on what you intend to do 
3

cal函数返回的东西。

t = self.cal(t) 

这里使用的cal的结果,但cal没有return声明,从而返回的None默认。通过返回正确的值来修复它。

相关问题