Click here to Skip to main content
15,921,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to make a code that prints every other letter on a different line for a class, but nothing is being printed.

What I have tried:

Python
plainText="hellomynameisbob"
railOne=""
railTwo=""
i=0
while i>=len(plainText):
  railOne=railOne+plainText[i]+" "
  i=i+1
  railTwo=railTwo+" "+plainText[i]
  i=i+1
print(railOne)
print(railTwo)
Posted
Updated 11-Jul-17 22:04pm
v3

Python
i=0
while i>=len(plainText):

Will do nothing, since 0 can never be greater than or equal to the length of the string.
 
Share this answer
 
Comments
Member 13304887 12-Jul-17 14:51pm    
I just changed it to this:
plainText="hellomynameisbob"
railOne=""
railTwo=""
i=0
while i<len(plainText):
railOne=railOne+plainText[i]+" "
i=i+1
railTwo=railTwo+" "+plainText[i]
i=i+1
print(railOne)
print(railTwo)

now it gives me this error:
Traceback (most recent call last):
File "C:/Users/maxwe/AppData/Local/Programs/Python/Python36-32/test2.py", line 8, in <module>
railTwo=railTwo+" "+plainText[i]
IndexError: string index out of range
Richard MacCutchan 13-Jul-17 3:20am    
That code works fine for me. You need to ensure that your plainText string contains an even number of characters.
Looks like your program takes chars from plaintext past its end.
In a string of length x, the chars are at positions from 0 to x-1.

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
Member 13304887 11-Jul-17 20:53pm    
I tried the debugger and I don't understand what it is saying at all
here is what the debugger gave me:
> /tmp/sessions/62a6cfe655950bdf/main.py(3)<module>()
-> plainText="hellomynameismax"
(Pdb) n
> /tmp/sessions/62a6cfe655950bdf/main.py(4)<module>()
-> railOne=""
(Pdb) n
> /tmp/sessions/62a6cfe655950bdf/main.py(5)<module>()
-> railTwo=""
(Pdb) n
> /tmp/sessions/62a6cfe655950bdf/main.py(6)<module>()
-> i=0
(Pdb) n
> /tmp/sessions/62a6cfe655950bdf/main.py(7)<module>()
-> while i>=(len(plainText)-1):
(Pdb) n
> /tmp/sessions/62a6cfe655950bdf/main.py(12)<module>()
-> print(railOne)
(Pdb) n

> /tmp/sessions/62a6cfe655950bdf/main.py(13)<module>()
-> print(railTwo)
(Pdb) n

--Return--
> /tmp/sessions/62a6cfe655950bdf/main.py(13)<module>()->None
-> print(railTwo)
(Pdb) n
Patrice T 11-Jul-17 21:15pm    
You should find a tutorial for the debugger. I don't practice python or this debugger.
you have groups of 3 lines:
/tmp/sessions/62a6cfe655950bdf/main.py is your file name.
(4) line number.
plainText="hellomynameismax" source code

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