python计算字符串中的字母而无计数功能 [英] python counting letters in string without count function

查看:130
本文介绍了python计算字符串中的字母而无计数功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序来计算没有计数功能的字符串中特定字母的出现.我将字符串放入列表中并设置了一个循环进行计数,但计数从未改变,我无法弄清原因.这就是我现在所拥有的:

I am trying to write a program to count the occurrences of a specific letter in a string without the count function. I made the string into a list and set a loop to count but the count is never changing and i cant figure out why. This is what I have right now:

letter = 'a'
myString = 'aardvark'
myList = []

for i in myString:
    myList.append(i)

count = 1

for i in myList:
    if i == letter:
        count == count + 1

    else:
        continue

print (count)

非常感谢您的帮助.

推荐答案

请注意,您正在使用count == count + 1,并且必须使用count = count + 1

Be careful, you are using count == count + 1, and you must use count = count + 1

要赋予新值的运算符是=,运算符==用于比较两个值

The operator to attribute a new value is =, the operator == is for compare two values

这篇关于python计算字符串中的字母而无计数功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆