我是靠谱客的博主 着急樱桃,这篇文章主要介绍python输出其中字母a的出现次数,Python程序如何计算字母c出现的次数,现在分享给大家,希望可以做个参考。

Assignment:

Return the number of occurrences of character c in string s,

ignoring case. Use loops. Do not use the in-built string method count,

which does a similar thing. The idea is to learn to write loops. You

should ignore case when comparing a character of s with c.

My attempt:

def countletter(s, c): #BAD

count = 0

for c in s:

if c == c:

count += 1

return count

Am I on the right track? I seem to get some assertion errors when I test it in the main...

解决方案

your return is at wrong place. So your function is actually returning only after one iteration.

Also you should not use the variable name c in for loop, use some different variable, as it replaces the value of c recieved from the function call with the current character being fetched by the for-loop.

def countletter(s, c): #BAD

count = 0

for x in s:

if x.lower() == c.lower():

count += 1

return count

print countletter("abcdefFf","F") #prints 3

print countletter("a","A") #prints 1

最后

以上就是着急樱桃最近收集整理的关于python输出其中字母a的出现次数,Python程序如何计算字母c出现的次数的全部内容,更多相关python输出其中字母a内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(120)

评论列表共有 0 条评论

立即
投稿
返回
顶部