Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么While循环在下一个语句中创建语法错误_Python_While Loop - Fatal编程技术网

Python 为什么While循环在下一个语句中创建语法错误

Python 为什么While循环在下一个语句中创建语法错误,python,while-loop,Python,While Loop,嗨,我一直在尝试用Python2.7编写一个程序,它将一个单词作为输入,并输出一个单词中的字母数。起初它是工作的,但是发生了一些事情,现在它不断地在第一行返回一个错误,这不是while循环的一部分。 这是代码的一部分: def number_of_letters(input): nol = input.find(input[-1]) while input[nol:] != input[-1]: nol = input.find(input[-1], input.

嗨,我一直在尝试用Python2.7编写一个程序,它将一个单词作为输入,并输出一个单词中的字母数。起初它是工作的,但是发生了一些事情,现在它不断地在第一行返回一个错误,这不是while循环的一部分。 这是代码的一部分:

def number_of_letters(input):
    nol = input.find(input[-1])
    while input[nol:] != input[-1]:
        nol = input.find(input[-1], input.find(input[-1] + 1)
    nol = nol + 1
    print nol
python解释器通过我在while块之后尝试放置的任何内容(在本例中为'nol=nol+1')不断返回语法错误 我试过玩弄它,但没用。请帮忙。
顺便说一句,如果有任何模块可以帮助这个程序,那将是非常好的,但我也想知道为什么这个模块不起作用

nol = input.find(input[-1], input.find(input[-1] + 1)) #<- add here
如果你不在乎有什么字符,就用
len(inp)


避免将
input
作为变量名,因为它会影响python函数。

您缺少一个结束参数:

nol = input.find(input[-1], input.find(input[-1] + 1)) #<- add here
如果你不在乎有什么字符,就用
len(inp)

避免将
input
作为变量名,因为它会影响python函数。

更改此选项

nol = input.find(input[-1], input.find(input[-1] + 1)
对此

nol = input.find(input[-1], input.find(input[-1] + 1))
请注意结尾的括号。

更改此选项

nol = input.find(input[-1], input.find(input[-1] + 1)
对此

nol = input.find(input[-1], input.find(input[-1] + 1))

请注意末尾的括号。

有一个内置函数用于获取python中字符串的长度

word = "test"
length = len(word)

python中有一个内置函数用于获取字符串的长度

word = "test"
length = len(word)

谢谢,顺便说一句,我讨厌typos@JediPythonClone,不用担心,这是常有的事;)谢谢,顺便说一句,我讨厌typos@JediPythonClone,不用担心,这是常有的事;)感谢这么多它工作如此之多btw打字错误吸吮感谢这么多它工作如此之多btw打字错误吸吮OP可能只是为了他们自己的原因尝试创建自己的功能。他还要求提供任何有帮助的“模块”。OP可能只是为了他们自己的原因尝试创建自己的功能。他还要求提供任何“模块”那会有帮助的。在将来,如果能将SyntaxerError包含在问题中,那就太好了。还有它在哪条线上。我的翻译说语法错误在第5行,所以我检查了那一行和之前的那一行。看起来您在
while
循环中的行中缺少了一个右括号。我的没有说明它是哪一行-它在将来不断变化,如果能在问题中包含语法错误,那就太好了。还有它在哪条线上。我的翻译说语法错误在第5行,所以我检查了那一行和之前的那一行。看起来您在
while
循环中的行中缺少了一个右括号。我的没有说明它是哪一行-它一直在变化