420chan now has a web-based IRC client available, right here
Leave these fields empty (spam trap):
Name
You can leave this blank to post anonymously, or you can create a Tripcode by using the float Name#Password
Comment
[*]Italic Text[/*]
[**]Bold Text[/**]
[~]Taimapedia Article[/~]
[%]Spoiler Text[/%]
>Highlight/Quote Text
[pre]Preformatted & Monospace text[/pre]
1. Numbered lists become ordered lists
* Bulleted lists become unordered lists
File

Sandwich


Community Updates

420chan now supports HTTPS! If you find any issues, you may report them in this thread
Help me as I ask questions by Samuel Gunningtack - Wed, 23 Aug 2017 18:12:33 EST ID:FzfAQ7sK No.37162 Ignore Report Quick Reply
File: 1503526353355.gif -(2193920B / 2.09MB, 125x125) Thumbnail displayed, click image for full size. 2193920
>1. Goal = define a function that takes input and then returns it in reverse
>2. result https://pastebin.com/BqUXJMxP
>3. Question = I somehow got it to work, but I dont even know how. I intended for this to just print the last part of an input, but it actually did it and I dont understand how


for i in text:
new_string = i+new_string


someone explain the code logic for why this returns the input in reverse for me, thanks
>>
Shit Blackham - Thu, 24 Aug 2017 12:03:01 EST ID:8B+9eE7j No.37164 Ignore Report Quick Reply
1503590581119.jpg -(50795B / 49.60KB, 1024x434) Thumbnail displayed, click image for full size.
A good way to understand what's going on in code is to throw some output statements in there. Try this:

https://pastebin.com/SnShQ5qQ
>>
Charles Gallylat - Thu, 24 Aug 2017 12:05:26 EST ID:9QSfnS0r No.37165 Ignore Report Quick Reply
  • it start with an empty string:
  • it starts a loop over each character starting with one
  • takes a concatenation of character of the loop and the variable and stores it into the variable
    • it does that till there is no characters left

in python you'd actually can do:
"abcd"[::-1]
and get:
'dcba'
>>
Hedda Sevinglock - Sat, 02 Sep 2017 10:06:04 EST ID:umBY7e8A No.37174 Ignore Report Quick Reply
>>37162
Try this code visualizer: http://www.pythontutor.com/visualize.html#mode=edit
>>
Basil Duckspear - Fri, 08 Sep 2017 14:58:56 EST ID:2mg3P58s No.37183 Ignore Report Quick Reply
Inside of the reverse function your code creates an empty string and then for each character in the string provided as argument you are *prepending* the current character to the string that is defined in the function.

Say that you have the string "hello world", here's what happens:

you enter the reverse function and create an empty string ''

you take the first character of "hello world"; 'h' and put it in front of the empty string and then assign that to the variable that held the empty string. Now that variable holds "h".

Next iteration of the for-loop it takes the next character of the argument string, so 'e', and it puts that in front of what it has and it becomes "eh"

It then continues in this fashion until all of the characters of the argument string have been consumed.

"leh"

"lleh"

"olleh"

" olleh"

"w olleh"

"ow olleh"

"row olleh"

"lrow olleh"

"dlrow olleh"

Finally it returns the result


Report Post
Reason
Note
Please be descriptive with report notes,
this helps staff resolve issues quicker.