0

I'm having trouble getting a while loop to work here. What I would like to do is have the program jump back to "please enter the word you with to translate" once it runs and provides the output.

When I have used while true and continue in what I believe are the proper places, it simply continues to print the output. of the word I'm translating. Hope that makes sense.

Listed below is the code I have working. The second chunk is where I add the while loop and run into issues.

def silly_alpha():
    print("Looks like we're going with the Impossible alphabet.")
    word_trans = input('Please enter the word you wish to translate: ')
    if word_trans.isalpha():
        for letter in word_trans:
            print(impossible.get(letter.lower()), end=' ')
    else:
        print("There's no numbers in words. Try that again.")

This is the problematic code

def silly_alpha():
    print("Looks like we're going with the Impossible alphabet.")
    while True:
        word_trans = input('Please enter the word you wish to translate: ')
        if word_trans.isalpha():
            for letter in word_trans:
                print(impossible.get(letter.lower()), end=' ')
                continue
        else:
            print("There's no numbers in words. Try that again.")
            continue
4
  • 2
    You do not need to use continue at all. Commented Dec 14, 2016 at 9:12
  • What do you think continue do ? Commented Dec 14, 2016 at 9:12
  • The continue will continue with the most inner block. The for iteration in the if case and the while in the else case. Commented Dec 14, 2016 at 9:12
  • 2
    Mistook continue for break. Maybe break is what OP is looking for? Commented Dec 14, 2016 at 9:16

2 Answers 2

1

To have it repeat the loop, and accept a new word to translate, you simply need to remove those continue statements. I tested this in IDLE and it works just fine.

def silly_alpha():
    print("Looks like we're going with the Impossible alphabet.")
    while True:
        word_trans = input('Please enter the word you wish to translate: ')
        if word_trans.isalpha():
            for letter in word_trans:
                print(impossible.get(letter.lower()), end=' ')
        else:
            print("There's no numbers in words. Try that again.")

However, you now have an infinite loop. You may want to consider some way of allowing the user to enter a command that will terminate the loop. Perhaps something like:

def silly_alpha():
    print("Looks like we're going with the Impossible alphabet.")
    while True:
        word_trans = input('Please enter the word you wish to translate, "x" to cancel: ')
        if word_trans == 'x':
            print('Exiting translation...')
            break
        elif word_trans.isalpha():
            for letter in word_trans:
                print(impossible.get(letter.lower()), end=' ')
        else:
            print("There's no numbers in words. Try that again.")
Sign up to request clarification or add additional context in comments.

4 Comments

ahhhh this makes sense and is working. Thank you so much for the input!
while I do see the difference and see what has changed. Can you explain why place while True before word_trans makes the difference? When placed after it causes this looping issue, but when place before, it works as expected.
You repeatedly want to ask for an input, so each time the loop starts again you want to be able to enter something new, otherwise you can assign word_trans once and then it just loops for the same word_trans over and over again.
Got it, that makes perfect sense and now it seems like a dumb question. Thanks so much for the answer and help with understanding that.
1

continue applies to the closest loop and allow to skip next instructions in this loop.

So your first continue applies to for, as it s the last instruction of the loop, it as no effect.

You second continue applies to while True, as it s the last instruction of the loop, it as no effect.

What you are looking for is break which terminates the closest loop. In your case, the while True I suppose.

So remove the first continue and replace the second by a break.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.