0

I have written a small program in python to count the number of occurrences of a character in python.

def count_star():
fo=open("e:/txt/inpt.txt","r")
str=fo.read()
index=0
ca=0
cb=0
cc=0
for letter in str:
    if letter == 'a' or 'A':
        ca=ca+1
    elif letter == 'b' or 'B':
        cb=cb+1
    elif letter == 'c' or 'C':
        cc=cc+1
print(ca)
print(cb)
print(cc)

Output: 6 0 0 Input file: bcaaac

the problem is that it should print 3 1 2 but it is not doing so, please suggest

  • 'or' does not work like in English. You need `if letter == 'a' or letter == 'A'` or the slightly more pythonic `if letter in ('a', 'A')` or the simpler `if letter.lower() == 'a'` – Max Feb 08 '14 at 22:11
  • `or` does not even work in English. I've asked people to clarify what they mean when they say in conversation, phrases that are similar to OP's code – inspectorG4dget Feb 08 '14 at 22:12

0 Answers0