# Help :D

#1

Can someone explain to me why this code actually works: I’m a little baffled.

def reverse(text):
word = “”
l = len(text) - 1
while l >= 0:
word = word + text[l]
l -= 1
return word

#2

It’s a loop that is taking the last letter of a given string called text and appending it to a blank string named word.

Take the length of a given word
`apple`
length of 5
subtract one
length is now 4
Remember that computers start counting from zero

``````a   p   p   l   e
0   1   2   3   4
``````

take the blank string word and add index 4 to it, then 3, then 2, then 1, then zero, then stop the loop

Problem with 5, reverse exercise
#3

Thanks a lot man. So which exact part of the code invokes the ‘‘backwards’’ part. is it l = len(text) - 1…?

#4

part of it, most of it is here:

``````while l >= 0:
``````

where we use `l`. This will loop from right hand-side (l, length of string -1 ) to left-side of the string (0)

#5

Thanks, I’m struggling to pick this up. This course is great but I feel I may have to restart it a few times. This is my first language so I’m just trying to get used to it

#6

you could always add a function call:

``````def reverse(text):
word = ""
l = len(text) - 1
while l >= 0:
word = word + text[l]
l -= 1
return word

print reverse("world!")
``````

then you can add print statements inside your code to “see” your code in action. Tools like this:

http://www.pythontutor.com/visualize.html#mode=edit

might help even more.

Also, practice makes perfect is challenging, its the next step, which is problem solving

#7

Thats a great tool! However I am still trying to define in my head which exact part of the code says ‘‘Start from the right instead of the left’’…what a nightmare. Sorry for all the questions, this is the most I’ve had to think in a while lol.

It really is challenging. I think I will reset my progress and start the course again until I am confident…do you know of any other decent resources that’ll teach me the ins and outs of median, sum ect ect as this course doesnt really go into too much detail. Would you recommend the intensive course for a beginner like me?

edit: when invoking while l >= 0: will it start from the highest number by default unless specified?

#8

well, maybe its wise to insert a print statement to see the value of l:

``````while l >= 0:
print l, text[l]
``````

#9

Remember that we set l to the length of the word minus one
In the case of apple that makes l equal to 4
Then we start a loop that says
While l is greater or equal to zero
add the lth (4th) index to new word
subtract one from l
l is now 3
add the lth (3rd) index to new word
subtract one from l

subtract one from l
l is now zero
add the zeroth index to new word
subtract one from l
l equals -1 which is less than zero
loop complete
return word

#10

Guys, thank you so much. Sorry for being a pain in the ■■■. I guess I just gotta keep practising and I guess, asking the experts on here

#11

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