Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when I do this:
plainText="dank memes"
i=0
railOne=""
railTwo=""
while i<len(plainText):
railOne+=plainText[i]+" "
i+=1
railTwo+=" "+plainText[i]
i+=1
print(railOne)
print(railTwo)
it works

but when I change nothing except remove the space between dank and memes is give me errors

What I have tried:

but when I change nothing except remove the space between dank and memes it gives me errors
Posted
Updated 12-Jul-17 9:38am
Comments
Richard MacCutchan 13-Jul-17 3:55am    
Because your code will only work with a string that is an even number of characters. Stepping through the code with a debugger, or just working through it with pen and paper, will show you why.

1 solution

Same problem as with Railfence code not printing anything Python[^]

You try to use chars past the end of the string.
Your code will crash every tome you have an odd number of chars because the loop use chars 2 by 2 but you don't check the there at least 2 remaining chars.
your loop must check that there at least 2 chars available.

The debugger would have showed you where and when is the problem.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
[no name] 12-Jul-17 15:40pm    
+5
Patrice T 12-Jul-17 15:43pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900