Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2012, 03:09 AM
renegade7 renegade7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 renegade7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old December 3rd, 2012, 06:00 AM
SuperOscar SuperOscar is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 1 m 41 sec
Reputation Power: 65
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)

Reply With Quote
  #3  
Old December 4th, 2012, 08:41 AM
renegade7 renegade7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 renegade7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old December 4th, 2012, 10:01 AM
SuperOscar SuperOscar is online now
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 403 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 1 m 41 sec
Reputation Power: 65
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.

Reply With Quote
  #5  
Old December 5th, 2012, 09:58 AM
WynnDeezl WynnDeezl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Posts: 139 WynnDeezl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old December 5th, 2012, 11:34 AM
renegade7 renegade7 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 renegade7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 4 sec
Reputation Power: 0
tks alot

Reply With Quote
  #7  
Old December 5th, 2012, 07:36 PM
spudzy18 spudzy18 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 spudzy18 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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"])

Reply With Quote
  #8  
Old December 19th, 2012, 11:06 PM
eGrove eGrove is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 7 eGrove User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #9  
Old December 21st, 2012, 11:31 AM
Marbelous Marbelous is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 8 Marbelous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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???

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Counting numbers of "a" in a word.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap