Need for clarification in code of 7/15


#1

Hello, I used the following code to pass 7/15

def reverse(text):
  count =[]
  for a in range(0,len(text)):
    count.append(text[len(text)-(a+1)])
  return "".join(count)

However, there’s one thing that’s bugging me, which may be silly but I still can’t wrap my head around it. That is in the for loop, my last run of the loop (that is when a==len(text)) should give me a result of text[-1]. Why is this a valid result?


#2

range does not include its stop value, so the highest value of the loop is len(text)-1.


#3

Thank you very much.


#4

not that [-1] is an invalid result:

print "hello world"[-1]

it will just give us values from the right hand side, so as in my example above -1 will give d, -2 will give l and so on


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.