I'm learning python 3 and I'm having problems with string related excerises. One exericse I'm working on is to write a program that cycle through a string and dispaly it like this:

         s u p e r n a t u r a l
         u p e r n a t u r a l s
         p e r n a t u r a l s u



     # This my code so far: 
def main():
    first_Name = "s u p e r n a t u r a l"
    for i in range(0,len(first_Name), 2):
        print(first_Name[i:], first_Name)
main()

When I code my code the output is this:

s u p e r n a t u r a l s u p e r n a t u r a l
u p e r n a t u r a l s u p e r n a t u r a l
p e r n a t u r a l s u p e r n a t u r a l
e r n a t u r a l s u p e r n a t u r a l
r n a t u r a l s u p e r n a t u r a l
n a t u r a l s u p e r n a t u r a l
a t u r a l s u p e r n a t u r a l
t u r a l s u p e r n a t u r a l
u r a l s u p e r n a t u r a l
r a l s u p e r n a t u r a l
a l s u p e r n a t u r a l
l s u p e r n a t u r a l

Is another for loop needed?

No, not at all. Here's the modfied code that fixes this:

def main():
    first_Name = "s u p e r n a t u r a l" + " "
    for i in range(0,len(first_Name)+2, 2):
        print(first_Name[i:] + first_Name[:i])

main()

Basicly, you forgot about that range loops though all the numbers upto BUT NOT INCLUDING the upper limit. You see that I've accounted for this by adding 2. Next, since we want the second word to crawl in letter by letter, we need to slice off the start of the second word that can fit in the space left behind by the first word as it's removed bit by bit. Your comma also adds a space, and since we need that gone as well, I've inclued it with the word.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.