0

I am extremely new to programming so it's taking a lot to re-wire. my brain to think like a computer programmer.

I need to create a script in Python using a while loop that does this:

zebra arbez
ebraz zarbe
braze ezarb
razeb bezar
azebr rbeza

Keep in mind that the script should be able to do this with any word. For example, right now a = 'zebra'. If a = 'cat' the the script should look like:

cat tac
atc cta
tca cat

I've figured out how to do it with a for loop...just can't figure out exactly how to implement it with a while loop.

My for loop:

a = 'zebra'
for i in range(len(a)):
    print a[i:] + a[:i], (a[i:] + a[:i])[::-1]

If anyone could help me out, or give me hints...I'd really appreciate it! thanks!

3
  • You know how to do it with a for loop but can't figure out how to do it with a while loop? That seems odd to me since for loops and while loops are usually taught at the same time. Are you just having trouble with the syntax? If that's the case, then the answer will depend on the language. Unfortunately you forgot to mention in your question which language you're using. Commented Jan 23, 2011 at 1:51
  • 1
    Add the code for your best shot, and you will likely get help. Commented Jan 23, 2011 at 1:53
  • 1
    Consider posting your for loop solution. People are more likely to help with homework if it's clear you've made an attempt. Commented Jan 23, 2011 at 1:53

3 Answers 3

7
for (initialization_expression;loop_condition;increment_expression){
  // statements
}

is basically just a nice way of writing

initialize_expression;
while(loop_condition){
    // statements
    increment_expression
}

Deconstruct your for loops into this format and you should have your solution.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh i'm so sorry. I'm using Python. :( My for loop is as follows: a = 'zebra' for i in range(len(a)): print a[i:] + a[:i], (a[i:] + a[:i])[::-1] I dunno why i'm having trouble converting it over to a while loop! I've tried so many different variations but everytime i run it, nothing happens! :(
Ah ok. Consider what for i in range(len(a)) gives you: the first time it gives you 0, the second, 1, etc ... until it gives you len(a)-1. Can you rewrite the loop with a while so your i starts and zero, and then increments until len(a)-1?
0

Here is some code that will work for any sized word, using a While loop:

word = "alpha"
index = 0

while (index < len(word)):
    print word + "   " + word[::-1]
    word = word[1:] + word[0]
    index += 1

1 Comment

word[1:] + word[0] works for strings (a string-index produces a string) but will fail generally (a list-index is most often not a list); word[1:] + word[:1] is more generally correct.
0

The following code seems to work as well, but I am not sure if this is what your teacher wants

a = 'zebra'
b=''
while a: 
    print a+b, (a+b)[::-1]
    b+=a[0]
    a=a[1:]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.