要算一个字母在字符串中出现的次数,这里有问题.任何帮助
def countLetters(string, character):
count = 0
for character in string:
if character == character:
count = count + 1
print count
Run Code Online (Sandbox Code Playgroud)
其他人已经涵盖了你的功能错误.这是另一种做你想做的事情的方式.Python的内置字符串方法count()返回字符串的出现次数.
x = "Don't reinvent the wheel."
x.count("e")
Run Code Online (Sandbox Code Playgroud)
得到:
5
Run Code Online (Sandbox Code Playgroud)