Reputation: 3
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
Upvotes: 0
Views: 93
Reputation: 2490
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
.
Upvotes: 1
Reputation: 6596
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.")
Upvotes: 1