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
2 Answers 2
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.")
4 Comments
while True before word_trans makes the difference? When placed after it causes this looping issue, but when place before, it works as expected.word_trans once and then it just loops for the same word_trans over and over again.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.
continueat all.continuewill continue with the most inner block. Theforiteration in theifcase and thewhilein theelsecase.continueforbreak. Maybebreakis what OP is looking for?