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

Jed*_*one 1 python while-loop

嗨我一直在尝试在python 2.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
Run Code Online (Sandbox Code Playgroud)

python解释器通过我尝试在while块之后放置的任何东西(在这种情况下'nol = nol + 1')继续返回语法错误我试过玩它但没有任何效果.请帮忙.顺便说一句,如果有任何模块可以帮助这个程序,这将是伟大的,但我也想知道为什么这个不工作

Pad*_*ham 5

你错过了一个结束语:

nol = input.find(input[-1], input.find(input[-1] + 1)) #<- add here
Run Code Online (Sandbox Code Playgroud)

如果要计算可以使用的实际字母数str.isalpha:

return sum(ch.isalpha() for ch in inp)
Run Code Online (Sandbox Code Playgroud)

如果你不在乎什么字符只是使用len(inp).

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