the body of a question Implement the findChar_pos() function by modifying the findChar() function to satisfy the conditions below.
Add a position parameter that receives an integer. The integer transmitted through position indicates the location to start the search. That is, the previously defined findChar() function makes it a special case in which the position is 0.
def findChar_pos(word, char, pos):
idx = 0
while idx < len(word):
if word[idx] == char:
return idx
idx = idx + 1
return -1
assert findChar_pos("hello world", "o", 0) == 4
assert findChar_pos("hello world", "o", 5) == 7
------------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-161-bf375050267a> in <module>()
6 idx = idx + 1
7 return -1
----> 8 assert findChar_pos("hello world", "o", 0) == 4
9 assert findChar_pos("hello world", "o", 5) == 7
AssertionError:
I can't try to implement pos in an if word [idx:pos] way ㅜㅜ
Hello.
If you read the code of the description and the text carefully, it's actually a very simple problem. There's the answer to the question. Please find out how to apply the hint from the problem to the code in the notes you wrote down.
def findChar_pos(word, char, position): # Add a position parameter that receives an integer.
The integer transmitted through idx = position # position indicates the location to start the search.
while idx < len(word):
if word[idx] == char:
return idx
idx = idx + 1
return -1
The findChar
function repeats the idx to the length of the word and finds the character, so you can adjust the value of idx
to make it examine from that part.
© 2025 OneMinuteCode. All rights reserved.