#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0

    Counting numbers of "a" in a word.


    def countA(word):
    for i in range(0,len(word)):
    b=0
    if ord(word[i])==97:
    b=b+1
    return b
    else:
    return 0

    this is my code used to count the numbers of "a" in a word. it only count small letter a and not the capital letter A. the problem is that my code cannot count words that have more a than 1 like banana whereas apple worked fine.
    for example the word banana should output 3 instead my code showed only 0.Please help me thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by renegade7
    Code:
    def countA(word):
    	for i in range(0,len(word)):
    		b=0
    		if ord(word[i])==97:
    			b=b+1	
    			return b
    		else:
    			return 0
    the problem is that my code cannot count words that have more a than 1 like banana whereas apple worked fine.
    Well, take the return statements out of the loop! Now you return as soon as the first instance of a is found.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    Originally Posted by SuperOscar
    Well, take the return statements out of the loop! Now you return as soon as the first instance of a is found.
    what do u mean taking out the return. If i took the return out the function is still not working.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    428
    Rep Power
    66
    Originally Posted by renegade7
    what do u mean taking out the return. If i took the return out the function is still not working.
    I mean your function now returns as soon as it first executes b = b + 1. Dont return there. Move the return statement to the end of the function.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (mini laptop, work laptop), Android 4.2.1 (tablet)
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    Loop


    Also, need to take the b=0 out of the for loop.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    8
    Rep Power
    0
    tks alot
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    1
    Rep Power
    0
    Code:
    def word(x):
    	return len([letter for letter in x if letter in "aA"])
  14. #8
  15. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Re: To count number of "a" in a file


    def countA(word):
    count = 0
    for count, letter in enumerate(word):
    if count.lower() == "a":
    count += 1
    return count
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Tewksbury, MA
    Posts
    31
    Rep Power
    2
    @ eGrove: Why do you bother to dredge up these old threads that have already been answered? The original posters are long gone and if other people don't notice the dates they may waste their time posting useless answers just like you are.

    I've seen you do this over at Dream.in.Code too. Why not answer questions that NEED answering instead???

IMN logo majestic logo threadwatch logo seochat tools logo