Answer to Question #244145 in Python for sayem

Question #244145

What will be the for loop of this code? and can you tell a little description of the process by mentioning what does index and len mean here! Thank you.


def f3(word, letter):

  index = 0

  while index < len(word):

    if word[index] == letter:

      return index

    index = index + 1

  return -1


print(f3("processing", "s"))

print(f3("processing", "z"))



1
Expert's answer
2021-09-29T05:06:20-0400
def f3(word,letter):
    #index here is a counter variable used to ascertain if the condition is true or false
    index = 0
    #len is an inbuit python function that returns the length of a strig,list,e.t.c
    while index < len(word):
        if word[index] == letter:
            return index
        index = index + 1
    return -1
print(f3('processing', 's'))
print(f3('processing', 'z'))

def f3(word, letter):
    for i in word:
        if i == letter:
            return word.index(i)
    return -1
print(f3('processing', 's'))
print(f3('processing', 'z'))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS