-2

I am trying to count the number of occurrences of a specific letter(s) in a string. To give an example, I want to count the letter a and b in the string "acdjsfdklfdcfnqc" (String or the specific letters are not important).

Firstly, I defined a container c that will count the occurrences of the letters using a while loop and an if statement. My code is below:

def iterate_through_elements(input_str):
    c = 0
    for elements in input_str:
        if elements == "a":
            c += 1        
    return c

This code worked, here I am only trying to count the number of a's in my string, but when I wanted to count also the b's, I modified my code like above:

def iterate_through_elements(input_str):
    c = 0
    for elements in input_str:
        if elements == "a" or "b":
            c += 1        
    return c

I added an or operator, my idea was to count both the a's and the b's but instead my second function gave me the total number of letters in the string, like the len() function. Then I have managed to accomplish my goal using the function above:

def get_count(inputStr):
    num_vowels = 0
    for char in inputStr:
        if char in "ab":
           num_vowels = num_vowels + 1
    return num_vowels

My main question is: why my second code from top gave me the total number of letters in the string?

I expected it to iterate through every element and look for if it is a or b, If it was a or b increment the container (variable c) by one. I could not understood how the c was equal to the length of the string and why it worked for a single letter (first function above) and not for the second? What has changed?

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56

3 Answers3

2

You'll need to change

if elements == "a" or "b":

into

if elements == "a" or elements == "b":

or better:

if elements in "ab":

The problem with the line if elements == "a" or "b": is that python will check if elements == "a", or if the truthy value of "b" is True or False, which it will always be True1 *(an empty string ''isFalse`)*.

Red
  • 26,798
  • 7
  • 36
  • 58
  • Note that `if elements in 'ab':` is only equivalent to `if elements == "a" or elements == "b":` when it is guaranteed that `elements` is a string of exactly one character (which is the case here, as we get `elements` by iterating on a string). Otherwise, it would also be `True` if `elements` is `"ab"` or `""` (empty string) , as `"ab" in "ab"` is `True`, and `'' in "ab"` is also `True`. – Thierry Lathuille Mar 06 '21 at 17:37
  • @ThierryLathuille Of course, thanks. Though I never said otherwise, I was only referring to the OP's current code. – Red Mar 06 '21 at 17:56
2

The problematic line is the one below:

if elements == "a" or "b": 

The above line means if either element == "a" is True or just "b" is True and since "b" is always True, it always goes into the if block.

The right code should be:

if elements == "a" or elements == "b": 
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
1

Returns the amount of times a and b is in str

def get_Count(str) :
      x = str.count("a")
      y = str.count("b")
      return x + y
Red
  • 26,798
  • 7
  • 36
  • 58
JACKDANIELS777
  • 339
  • 1
  • 14