0

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?

2
Contributors
1
Reply
23
Views
3 Years
Discussion Span
Last Post by brett.warren.1612
2

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.

Edited by brett.warren.1612: Grammar

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.