The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Counting numbers of "a" in a word.
Discuss Counting numbers of "a" in a word. in the Python Programming forum on Dev Shed. Counting numbers of "a" in a word. Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 3rd, 2012, 03:09 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 1 h 19 m 4 sec
Reputation 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.
|

December 3rd, 2012, 06:00 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)
|

December 4th, 2012, 08:41 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 1 h 19 m 4 sec
Reputation Power: 0
|
|
Quote: | 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.
|

December 4th, 2012, 10:01 AM
|
|
Contributing User
|
|
Join Date: Jul 2007
Location: Joensuu, Finland
|
|
Quote: | 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”. Don’t return there. Move the return statement to the end of the function.
|

December 5th, 2012, 09:58 AM
|
|
Contributing User
|
|
Join Date: Aug 2011
Posts: 139
Time spent in forums: 2 Days 3 h 53 m 35 sec
Reputation Power: 2
|
|
|
Loop
Also, need to take the b=0 out of the for loop.
|

December 5th, 2012, 11:34 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 1 h 19 m 4 sec
Reputation Power: 0
|
|
tks alot 
|

December 5th, 2012, 07:36 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 1
Time spent in forums: 31 m 8 sec
Reputation Power: 0
|
|
Code:
def word(x):
return len([letter for letter in x if letter in "aA"])
|

December 19th, 2012, 11:06 PM
|
|
Permanently Banned
|
|
Join Date: Dec 2012
Posts: 7
Time spent in forums: 4 h 58 m 56 sec
Warnings Level: 10
Number of bans: 1
Reputation 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
|

December 21st, 2012, 11:31 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 8
Time spent in forums: 4 h 41 m 11 sec
Reputation Power: 0
|
|
 @ 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???
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|