#๐ so I need to use the .find() method to rewrite my count_letters function
44 messages ยท Page 1 of 1 (latest)
@mortal echo
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.
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.
What have you tried so far?
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?
im not really sure what the third paremeter is going to do
the assignments kinda confusing
You can always look at the documentation to see what arguments do https://docs.python.org/3/library/stdtypes.html#str.find
Here we see that the second argument defines at what index to start searching
i know what .find() does i just dont know what the 3rd parameter is going to do
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
so i need to make a new variable called str
str is built in, it's the string class
oh my bad
so do i need fruit in the parameter of the while loop or does it need something else with it
find will return -1 if it doesn't find anything, run the while loop untill you have the -1
what would i put in the index?
The point from wher eyou want to start your search
oh ok\
while fruit:
str.find(fruit, letter, index)
return fruit
print(count_letter('apple', 'p', 0))``` its just printing apple
Hey @mortal echo!
Add a py after the three backticks.
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You are not doing any counting
oh do i need count?
that is the purpose of the function
Yes, increment a counter every time you find something that's not -1
so if str.find(fruit, letter, index): counter count += 1?
That wouldn't work because all ints that are not 0 are True, so -1 is True as well
so i would need to make the counter += to -1? and then do count?
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.
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.
@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?
!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.