2

I'm just learning python and I came to a problem that required me to put a string as an input in my "count letters function."

The function is supposed to count how many "a"s are in a word given (specifically, a fruit inputed by the user).

The final value is correct, but in my function, it lists me the programs "procedure" if you will by listing how many "a"s are at each index and adding everything that was before.

I just want one final value, not a list.

If I'm not too clear, here is my program and test it out for yourself to see what I mean, and maybe help me come to a solution?

def count_letters(a):
    count=0
    for char in a:
        if char == 'a':
            count+=1
        print count

a=raw_input("Name a fruit and I will tell you how many letters are in it." + '\n')
print count_letters(a)
dot.Py
  • 5,007
  • 5
  • 31
  • 52

7 Answers7

2

Just change:

for char in a:
    if char == 'a':
        count+=1
    print count

To:

for char in a:
    if char == 'a':
        count+=1
print count

Otherwise you'll be printing the counter each time the for loop runs.


Also, you're calling your function with print at print count_letters(a).

You don't need this since you've put a print statement at the last line in your code. If you left the print statement there to call your function, it'll print None, since the function already have returned the count value.

So you can also change:

print count_letters(a)

To:

count_letters(a)

Output Example with print count_letters(a):

Name a fruit and I will tell you how many letters are in it. abacaxi 
3
None

Output Example with count_letters(a):

Name a fruit and I will tell you how many letters are in it. abacaxi 
3
dot.Py
  • 5,007
  • 5
  • 31
  • 52
1

If you want to keep a similar structure and calculate the count yourself, you can use :

def count_a(fruit_name):
    return sum(1 for char in fruit_name if char == 'a')

It's shorter, it's more pythonic, it's not a standard method call, it returns an integer instead of just printing it and the variable names might be a bit clearer.

If you don't mind using an existing python method, you can use :

return fruit_name.count('a')
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
0

You just put print function in for loop. Tray this:

def count_letters(a):
    count=0
    for char in a:
        if char == 'a':
        count+=1
    print count

Indentation is important in python

0

A simple change makes it work. Just replace the print by return and remove one indentation level to get it of of the loop.

def count_letters(a):
    count=0
    for char in a:
        if char == 'a':
            count+=1
    return count

In general, it is considered more pythonic to avoid for loops. While not the shortest solution (see comment by Eric Duminil) to your problem, this might give an idea how to use list comprehension instead:

def count_a(text):
    return len([x for x in text if x=='a'])

print count_a("banana")
Community
  • 1
  • 1
mikuszefski
  • 3,943
  • 1
  • 25
  • 38
0

It seems like you don't understand the difference between print and return. What you are doing is printing every single occurrence in the loop, when what you want is the final value at the end. What you would have to do is keep the same code, except when the loop terminates, you want to return whatever value is at the end. So get rid of print count, and remove the indent. Then you must return count. You don't want to return in the loop, because this will return the first value of count after a single character is checked. Message me for further questions if you have any.

DrJessop
  • 462
  • 6
  • 26
0

Try this:

Use a for loop, based on the word given by the Sphinx

word = input("Waiting for word from Sphinx... ")
word1 = word
for word in word1:
    print(word)
baduker
  • 19,152
  • 9
  • 33
  • 56
0

Your code is correct but here print condition should be under count_letters function not in the for loop

def count_letters(a):
    count=0
    for char in a:
        if char == 'a':
            count+=1
    print count

a=raw_input("Name a fruit and I will tell you how many letters are in it." + '\n')
print count_letters(a)
roschach
  • 8,390
  • 14
  • 74
  • 124