这段内容的翻译为: “能否有人解释,为什么这段代码不起作用”

huangapple go评论69阅读模式
英文:

can anyone explain , why this code is not working

问题

class Solution(object):
def lengthOfLastWord(self, s):
"""
:type s: str
:rtype: int
"""
len = 0
res = 0
print(len(s))
for i in range(len(s)):
if s[i] != " ":
len += 1
else:
res = len
len = 0
if len != 0:
return len
else:
return res
nitesh = Solution()
s = 'Hello World'
print(nitesh.lengthOfLastWord(s))


Error:

Traceback (most recent call last):
File "d:\dsa\python\58.py", line 22, in
print(nitesh.lengthOfLastWord(s))
File "d:\dsa\python\58.py", line 9, in lengthOfLastWord
print(len(s))
TypeError: 'int' object is not callable
PS D:\dsa\python>



<details>
<summary>英文:</summary>


class Solution(object):
def lengthOfLastWord(self,s):
"""
:type s: str
:rtype: int
"""
len=0
res=0
print(len(s))
for i in range(len(s)):
if s[i] !=" ":
len+=1
else:
res=len
len=0
if len!=0:
return len
else:
res
nitesh=Solution()
s='Hello World'
print(nitesh.lengthOfLastWord(s))





Error:
```
Traceback (most recent call last):
  File &quot;d:\dsa\python.py&quot;, line 22, in &lt;module&gt;
    print(nitesh.lengthOfLastWord(s))
  File &quot;d:\dsa\python.py&quot;, line 9, in lengthOfLastWord
    print(len(s))
TypeError: &#39;int&#39; object is not callable
PS D:\dsa\python&gt;
```

</details>


# 答案1
**得分**: 3

因为您在方法内部定义了另一个变量`len=0`。

`len(s)` 现在会导致使用变量`s`调用方法,该变量为整数。

<details>
<summary>英文:</summary>

It&#39;s because you&#39;ve defined another variable `len=0` inside the method.

`len(s)` would now result in calling method with variable `s` on an `int`.

</details>



# 答案2
**得分**: 0

Name your variables appropriately.
Don't Name your variable with reserved keywords or built-in functions.

```python
class Solution(object):
    def lengthOfLastWord(self, s):
        """
        :type s: str
        :rtype: int
        """
        length = 0
        res = 0
        print(len(s))
        for i in range(len(s)):
            if s[i] != " ":
                length += 1
            else:
                res = length
                length = 0
        if length != 0:
            return length
        else:
            res

nitesh = Solution()
s = 'Hello World'
print(nitesh.lengthOfLastWord(s))
```

<details>
<summary>英文:</summary>

Name your variables appropriately.
Don&#39;t Name your variable with reserved keywords or built-in functions.

```
class Solution(object):
    def lengthOfLastWord(self, s):
        &quot;&quot;&quot;
        :type s: str
        :rtype: int
        &quot;&quot;&quot;
        length = 0
        res = 0
        print(len(s))
        for i in range(len(s)):
            if s[i] != &quot; &quot;:
                length += 1
            else:
                res = length
                length = 0
        if length != 0:
            return length
        else:
            res


nitesh = Solution()
s = &#39;Hello World&#39;
print(nitesh.lengthOfLastWord(s))

```

</details>



# 答案3
**得分**: 0

如果你将变量命名为保留关键字或内置函数的名称,那么将覆盖该关键字或函数的定义。因此,当你使用该关键字或函数时,它将不按照原始关键字或函数的方式工作,而只会作为变量使用... 这就是为什么会出现错误,错误消息说 int 对象不可调用... 请永远记住,当错误包含可调用的词时,它是关于 (),而如果错误包含可订阅的词,它是关于 [],因此相应地找到你的解决方案。

<details>
<summary>英文:</summary>

If you name your variable as the name of reserved keyword or built in function then that will overwrite the definition of that keyword or function. So, when you use that keyword or function that will not work according to original keyword or function. It will work as variable only... That&#39;s why that error is saying, int object is not collable...  Always remember when error has collable word it is about (), and if error has subscriptible word it is about [] so accordingly find your solution. 

</details>



huangapple
  • 本文由 发表于 2023年4月4日 13:22:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定