Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
The following program does not run. Please don't ask me to debug the code myself. I am unable to.

Python
def find(word,letter):
    index=0
    while index <len(word):
        if word[index]==letter:
            return index
            index=index
print (index)

find("mississippi", "s")


What I have tried:

I am trying to count the number of times the letter s appears in the word Mississippi.
Posted
Updated 24-Oct-23 0:43am
v2

Yes because unless the first character in the word matches the letter, the while loop will continue until the end of time. I don't expect you want to wait that long.

You need to increment the index in your while loop each time you test a letter. It would also be a good idea to count the number ofletters that youfind. So something like:
Python
def find(word,letter):
    index=0
    count = 0 # set a counter
    while index <len(word):
        if word[index]==letter: # if the letter matches
            count += 1          # add 1 to count
        index += 1        # increment the index so we test all letters
    return count          # when all have been tested return the count

number = find("mississippi", "s")
print('There are', number, '"s" characters')

number = find("mississippi", "z")
print('There are', number, '"z" characters')
 
Share this answer
 
Comments
CPallini 24-Oct-23 7:45am    
5.
Quote:
The following program does not run. Please don't ask me to debug the code myself. I am unable to.
Which means "I found this on the internet, and I can't be bothered to work out how it works - so fix it for me" without telling us anything about what you expected it to do (or why you are bothering with faulty code in the first place).

That's a poor attitude, and one which will get you to eventually fail the course: when you come to the final exam, you won't have access to ChatGPT (or whatever else generated that drivel) and you will have no idea how to do it for yourself. Hence: a fail.

Do yourself a favour, and write your own code to solve your homework assignment: you will learn as you do, and then there is a good chance that you will pass instead ...
 
Share this answer
 
Comments
Baziga 26-Oct-23 6:17am    
I honestly feel discouraged after reading your reply! Someone who is entirely new to Python cannot learn to debug right in the beginning. If you don't want to reply to my questions, please ignore them.
OriginalGriff 26-Oct-23 6:31am    
You are joking, right?
Debugging is how you learn what works and what doesn't; you can't delegate it to others because if you do, you won't ever learn how to do it - and it's much, much easier to learn when you are dealing with 8 lines of code than when you have 8000, or 800,000 lines to find a bug in!

How do you expect to get any code working properly if you won't learn to fix it yourself?
Baziga 26-Oct-23 6:43am    
You earlier sent me a write-up on Debugging but I could not understand it. If possible, please guide me one more time on how can I learn to debug the code. Please share a link to a good video that teaches debugging.Would be very grateful! At present, my thinking is that debugging comes with practice. It can't be learned right in the beginning.
OriginalGriff 26-Oct-23 8:49am    
Debugging is just the name for something you probably do in the real world: working out why something happened. You look at what did happen, and decide how that differs from what you expected to happen. You turn on your PS5 and try to play a game, but it doesn't work. What's wrong?
That's the problem description, but in the real world you wouldn't start there: you'd think "did I press the button hard enough?" "Is it plugged in?" "Is the power on?" "Is the router working?" and you'd check those things before you started asking your mate questions.
That's the start of debugging: looking at the problem and identifying symptoms and their possible causes, then eliminating the "false leads" by gathering evidence that says that they aren't the reason: The light works, so the power is on. the plug is in the wall socket, and the other end in in the PS5, so it's not that. And so on: working away at one possible cause at a time until bingo! Your kid brother smashed the router with a hammer to annoy you, and that why your PS5 isn't working.

It's the same with software: you look at what code does do, and compare that with what you expect it to do. The debugger is just a tool you can use to "fine grain" the amount of code you are debugging at a time right down to an individual line of code at a time.

Give it a try - and no, I'm not going to try and wade through the steaming pile of dross that is anything computer related on YouTube to find a "debugging video" that isn't total crap for you. Life is too short for that to be an efficient use of my time!
In the current functionality, the function find is intended to find the index of a particular letter in a given word. To count the number of occurrences of a letter in a given word, you can modify the find function to iterate through the entire word and increment a counter each time the desired letter is found. Here's an updated version of find function:
Python
def find(word, letter):
    count = 0
    for char in word:
        if char == letter:
            count += 1
    return count

result = find("mississippi", "s")
print(result)
 
Share this answer
 
Richard already gave you the correct solution.
As an alternative, you may use the count method of the string type:
Python
number = "mississippi".count("s")
print('There are', number, '"s" characters')
 
Share this answer
 
v2
Comments
Richard MacCutchan 24-Oct-23 9:14am    
5. But I think the OP is supposed to write some actual code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900