Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 正在尝试获取str中的字母数_Python - Fatal编程技术网

Python 正在尝试获取str中的字母数

Python 正在尝试获取str中的字母数,python,Python,我只是在做一个小函数,它会提示用户输入一个单词和一个字母,这个函数应该返回给定单词中该字母出现的次数 例如,我想让我的函数做的是: please enter a word: 'hello' please enter a letter: 'l' 2 我的代码如下: def num_char(): char = input('please enter a word: ') char_to_find = input('please enter a letter: ') num

我只是在做一个小函数,它会提示用户输入一个单词和一个字母,这个函数应该返回给定单词中该字母出现的次数

例如,我想让我的函数做的是:

please enter a word: 'hello'
please enter a letter: 'l'
2
我的代码如下:

def num_char():
    char = input('please enter a word: ')
    char_to_find = input('please enter a letter: ')
    num = 0    
    for i in char:
        if i == char_to_find:
            num += 1
    return num

if __name__ == '__main__':
    num_char()

问题是,当我运行它时,它只会提示用户,但之后不会做任何事情。你知道代码出了什么问题吗?

你实际上没有打印任何东西<代码>打印您的
返回的

def num_char():
    char = input('please enter a word: ')
    char_to_find = input('please enter a letter: ')
    num = 0    
    for i in char:
        if i == char_to_find:
            num += 1
    return num

if __name__ == '__main__':
    print num_char()  #for python-2.7
    print(num_char()) #for python-3.x

你实际上没有打印任何东西<代码>打印您的
返回的

def num_char():
    char = input('please enter a word: ')
    char_to_find = input('please enter a letter: ')
    num = 0    
    for i in char:
        if i == char_to_find:
            num += 1
    return num

if __name__ == '__main__':
    print num_char()  #for python-2.7
    print(num_char()) #for python-3.x

你实际上没有打印任何东西<代码>打印您的
返回的

def num_char():
    char = input('please enter a word: ')
    char_to_find = input('please enter a letter: ')
    num = 0    
    for i in char:
        if i == char_to_find:
            num += 1
    return num

if __name__ == '__main__':
    print num_char()  #for python-2.7
    print(num_char()) #for python-3.x

你实际上没有打印任何东西<代码>打印您的
返回的

def num_char():
    char = input('please enter a word: ')
    char_to_find = input('please enter a letter: ')
    num = 0    
    for i in char:
        if i == char_to_find:
            num += 1
    return num

if __name__ == '__main__':
    print num_char()  #for python-2.7
    print(num_char()) #for python-3.x


在我的电脑上工作。如果你在做家庭作业,这可能对你没有帮助,但在生产代码中,你可能希望使用
str.count
而不是手动循环。在我的电脑上工作。如果你在做家庭作业,这可能对你没有帮助,在生产代码中,您可能希望使用
str.count
而不是手动循环。在我的计算机上工作。虽然这可能对您做家庭作业没有帮助,在生产代码中,您可能希望使用
str.count
而不是手动循环。在我的计算机上工作。虽然这可能对您做家庭作业没有帮助,在生产代码中,您可能希望使用
str.count
而不是手动循环。您的第二个想法似乎更像python——如果代码作为模块调用,您可能需要返回值……感谢它现在实际返回了一些东西。不幸的是,它只返回0..有什么原因吗?@user305527,请尝试将您的输入转换为
str
as
str(输入('please enter a word:'))
,对于另一个输入也是如此哦哇。。为什么我必须使用str函数?如果用户输入一个str,那么它不应该在str上迭代吗?抱歉,我是新手。@user305527,实际上是
python-2.7中的
raw\u input()
做的。在
python-3.x
中,输入也可以是表达式,因此您必须告诉解释器将其解释为什么。希望这有助于您的第二个想法看起来更像python——如果代码作为模块调用,您可能需要返回值……谢谢,它现在实际上返回了一些东西。不幸的是,它只返回0..有什么原因吗?@user305527,请尝试将您的输入转换为
str
as
str(输入('please enter a word:'))
,对于另一个输入也是如此哦哇。。为什么我必须使用str函数?如果用户输入一个str,那么它不应该在str上迭代吗?抱歉,我是新手。@user305527,实际上是
python-2.7中的
raw\u input()
做的。在
python-3.x
中,输入也可以是表达式,因此您必须告诉解释器将其解释为什么。希望这有助于您的第二个想法看起来更像python——如果代码作为模块调用,您可能需要返回值……谢谢,它现在实际上返回了一些东西。不幸的是,它只返回0..有什么原因吗?@user305527,请尝试将您的输入转换为
str
as
str(输入('please enter a word:'))
,对于另一个输入也是如此哦哇。。为什么我必须使用str函数?如果用户输入一个str,那么它不应该在str上迭代吗?抱歉,我是新手。@user305527,实际上是
python-2.7中的
raw\u input()
做的。在
python-3.x
中,输入也可以是表达式,因此您必须告诉解释器将其解释为什么。希望这有助于您的第二个想法看起来更像python——如果代码作为模块调用,您可能需要返回值……谢谢,它现在实际上返回了一些东西。不幸的是,它只返回0..有什么原因吗?@user305527,请尝试将您的输入转换为
str
as
str(输入('please enter a word:'))
,对于另一个输入也是如此哦哇。。为什么我必须使用str函数?如果用户输入一个str,那么它不应该在str上迭代吗?抱歉,我是新手。@user305527,实际上是
python-2.7中的
raw\u input()
做的。在
python-3.x
中,输入也可以是表达式,因此您必须告诉解释器将其解释为什么。希望有帮助