7. Reverse - For loop problem(?)


#1

I'm not sure what's happening in my code. But I'm trying the excercice in notepad++ and load it into python terminal. [code is not complete, I'm just not sure why I get this output]

  • When I import the code it prints all the "print" after the for loop like it is in the for loop for. I assume it's because it has text[c] in one print (I have no clue why it would print the other one) and c is defined in the for-loop, but when I add it to the loop I get an "unexpected indent" Error.

  • When I remove the last "print"-line [print "not in for loop??"] and reload the file back into python the for loop only runs once. Because this makes no sense for me.

def reverse(text):
    new = ()
    for x in range(1,len(text)+1):
        c = len(text) - x
        print c
    print "\t", text[c]
    print "not in for loop??"
    return

reverse("abcd")

output:

3
    d
not in for loop??
2
    c
not in for loop??
1
    b
not in for loop??
0
    a
not in for loop??

#2

Very important that your indents are 4 spaces per level of indentation. Please edit your post and format the code sample so we see the correct indentation.

How to format code samples in forum posts

Fix your post and ping this thread so we can return for a better look. Thank you.


#5

ok thanks. finally found the problem, I think I was using the wrong characters to have the proper formatting.

I also retried the code again and now my output seems to be correct (as in it's not adding the print functions in the for-loop.) Not sure how that happened since it should be the same function I tried when I posted this. Guess I made a mistake somewhere in the code, just not sure what It could have been.