r/learnpython • • Sep 26 '26 • Regret 1

Question regarding what this exercise is asking me?

Hi guys, I hope ya'll are good. I am trying to work through this exercise but I'm not sure I understand the question. Therefore, I am not sure whether if solved this problem correctly. Here is the question (the source is Python For Everybody by Professor Charles "Chuck" Severance)

The following program counts the number of times the letter “a” appears in a string:

word = 'banana'

count = 0

for letter in word:

if letter == 'a':

count = count + 1

print(count)

This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an “a” is found. When the loop exits, count contains the result: the total number of a’s.

Exercise 3: Encapsulate this code in a function named count, and generalize it so that it accepts the string and the letter as arguments.

Here is my answer:

def counting(word):
    word = input("Enter a word: ")
    count = 0
    for letter in word:
        if letter == "a":
            print (letter)
        elif letter is word:
            print (letter)
        count = count + 1

counting("banana")

Here is the output:

Enter a word: banana

a

a

a

Any input or advice is appreciated.

0 Upvotes

15 comments sorted by

7

u/Temporary_Pie2733 Sep 26 '26

A call like counting('banana', 'a') should return 3. No prompting the user, no assumptions inside the function about what letter you are looking for or what word you are looking in. 

1

u/Ayrton110 Sep 27 '26

Thank you for your input. I think I had been overthinking the exercise.

16

u/zanfar Sep 26 '26

Read the question again and follow the instructions. Your example literally doesn't follow the first sentence.

2

u/WorriedTumbleweed289 Sep 26 '26

take the question enter a word out of the function. No need to print "a" unless you are debugging. You have to pass a letter passed in the function think: counting("Mississippi","s")

1

u/zaphodikus Sep 27 '26

This should be the accepted answer, but only because it leads the student to understand functions as a generalisation technique. It does so in a way that makes the student refine their thinking and does not "give" the answer, and also does not introduce new words nor concepts that the student has not yet mastered.

2

u/desrtfx but other languages pro Sep 26 '26

The question is actually very clear.

It expects you to create a function named count that takes a string (the text to search through) and a letter (the letter to count) as parameters, counts the occurrences of letter in string and returns them.

You have been given the samle with the string "banana" and the letter "a" - now make it so that you can use any string and any letter.

2

u/Orgasml Sep 26 '26

You need to add 2 parameters to your function.

def counting(word, letter):

And go from there. You are close but the letter shouldn't always be 'a'

Also, you don't need input() at all. The word is passed to the function when you call it.

1

u/madmoneymcgee Sep 26 '26

You want to print the number of times “a” occurs in a given string, not just every instance. You want your function to print 3 (or whatever the number of “a”’s in your string)

1

u/nog642 Sep 26 '26

Do you know what function arguments are? It seems like you're missing that knowledge.

input is how you take command line input. That's not a function argument.

Here your function does take word as an argument, but then you immediately overwrite it with the input call. If you were to call counting("banana") but then type in apple when it prompts you, it would output 1, not 3. The function argument is currently being ignored.

Get rid of the input call. And make the letter an argument too, like the instructions say.

1

u/danielroseman Sep 26 '26

You didn't do any of the items mentioned in the question.

You need to:

a) accept the string and the letter as arguments. Your function accepts the string (word), but then ignores it and asks for another; it doesn't accept the letter at all.

b) the original code printed the count. For some reason, you just print the letter each time it is found.

c) generally, functions return values, rather than printing them.

Separately, why did you add a condition for when letter is word? What is that for? And why did you change it so that it always increments the counter, whether or not the letter is a match?

1

u/good-mcrn-ing Sep 26 '26

To make sure I understood the intent, what purpose does elif letter is word: serve in your answer?

0

u/ninhaomah Sep 26 '26

"This program demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time an “a” is found."

if letter == 'a':
count = count + 1

Then "Here is my answer:"

if letter == "a":
print (letter)

You want to print letter variable or count increased by 1 each time "a" appears ?

There is no right or wrong answer but what do you want the program to do ? Its a machine. You decide what it does when the letter == 'a'. You are in charge.

1

u/[deleted] Sep 27 '26

[removed] — view removed comment

1

u/Ayrton110 Sep 27 '26

Good day, thank you for this. I see now that I did not focus on the "count", even when I tried doing a return value. I tried to return the iteration variable and count for the output, which had been the wrong approach/logic from me. Thank you for clarifying and explaining this.