Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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程序没有';行不通_Python_Python 2.7 - Fatal编程技术网

python程序没有';行不通

python程序没有';行不通,python,python-2.7,Python,Python 2.7,我试图用字符串返回一些字母,但我不明白为什么它不起作用。在字符串“mldtmgm”中,它应该返回3,因为顺序find应该返回字符串中所有m的总和。它返回0,我不知道这是否是因为使用find或其他方法不好 def ex4(str4): someLetter = str4[ :1] return str4.find(someLetter) print ex4("mldtmgm") str.find查找项目的索引。您需要使用str.count def ex4(str4):

我试图用字符串返回一些字母,但我不明白为什么它不起作用。在字符串
“mldtmgm”
中,它应该返回3,因为顺序
find
应该返回字符串中所有m的总和。它返回0,我不知道这是否是因为使用
find
或其他方法不好

def ex4(str4):

    someLetter = str4[ :1]
    return str4.find(someLetter)

print ex4("mldtmgm")

str.find
查找项目的索引。您需要使用
str.count

def ex4(str4):

    someLetter = str4[ :1]
    return str4.count(someLetter)

print ex4("mldtmgm")

正确描述您的问题。您可以使用
返回str4.count(someLetter)