#๐Ÿ”’ so I need to use the .find() method to rewrite my count_letters function

44 messages ยท Page 1 of 1 (latest)

mortal echo
#

the rewritten fuction```py
def count_letter(fruit, letter):

count = 0
while fruit:
    fruit.find(letter)

py

def count_letter(fruit, letter):

count = 0
for char in fruit:
    if char == letter:
        count += 1
return count

print(count_letter( 'apple', 'a'))``` orignal fuction

limber totemBOT
#

@mortal echo

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mortal echo
#

heres the assignment

#

rewrite the count_letters function so that instead of traversing the
string, it repeatedly calls the butiln find method, with the optional third parameter to locate new occurrences of the letter being counted.

idle plinth
#

What have you tried so far?

mortal echo
#

well i know i need a while loop

#

and i think its fruit.find(letter) right?

idle plinth
#

That will find the first occurence of that letter

#

Notice that the problem statement talks about an optional extra parameter

#

Do you know what this one does?

mortal echo
#

im not really sure what the third paremeter is going to do

#

the assignments kinda confusing

idle plinth
#

Here we see that the second argument defines at what index to start searching

mortal echo
#

i know what .find() does i just dont know what the 3rd parameter is going to do

idle plinth
#

They might want you to use it like this
str.find(fruit, letter, index)
instead of
fruit.find(letter, index)
these do the same ting

mortal echo
#

so i need to make a new variable called str

idle plinth
#

str is built in, it's the string class

mortal echo
#

oh my bad

#

so do i need fruit in the parameter of the while loop or does it need something else with it

idle plinth
#

find will return -1 if it doesn't find anything, run the while loop untill you have the -1

mortal echo
#

what would i put in the index?

idle plinth
#

The point from wher eyou want to start your search

mortal echo
#

oh ok\

#
    
    
    while fruit:
        str.find(fruit, letter, index)
        return fruit
print(count_letter('apple', 'p', 0))``` its just printing apple
limber totemBOT
#

Hey @mortal echo!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
idle plinth
#

You are not doing any counting

mortal echo
#

oh do i need count?

idle plinth
#

that is the purpose of the function

mortal echo
#

sorry im dumb

#

i need a if statement dont i

idle plinth
#

Yes, increment a counter every time you find something that's not -1

mortal echo
#

so if str.find(fruit, letter, index): counter count += 1?

idle plinth
#

That wouldn't work because all ints that are not 0 are True, so -1 is True as well

mortal echo
#

so i would need to make the counter += to -1? and then do count?

idle plinth
#

You'd need to check for the -1 in the if

#

I have to leave now.

#

One last thing I'll say is to keep track of the index that was found, that way you can use it in the next search.

sinful marsh
# mortal echo so if str.find(fruit, letter, index): counter count += 1?

Always be explicit in what you're testing. You suggested:

if find(......):

as though find was a true/false (aka Boolean) function which returned whether the thing was found. But when you read its docs, it returns the index of where the thing was found. And -1 when not found.
So be overt about it. If -1 is "not found", you want:

if find(......) != -1:
    count += 1

i.e. that the return value is not equal to the "not found" return value.

latent birch
#

@mortal echo additionally, you want to keep track of the index to know where to start the search the next iteration through the loop

it might be easier to not call the str.find() method in the if condition or you will have to use the walrus operator and an additional set of parenthesis around the assignment if you are going to keep it within the if condition without having to call str.find() more than one time per iteration

#

@mortal echo still there?

mortal echo
#

!close

limber totemBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.