0

So this code has worked correctly without the try clause, but I need it to work in this manner because I want to continue prompting the user for input and give an option to exit. Ive searched and cant find a similar questions asked. Can someone please take a look and see what Im missing, thanks.

vowels = 'aeiou'

def converter(word):
    while True:
        try:
            first = word[0] 
            if first in vowels:
                word = word + 'yay'
                return word
            else:
                while word[0] not in vowels:
                    word = word[1:] + word[0]
                word = word + 'ay'
                return word
    
            split = word.split()
             
            translated = [converter(word) for word in split]
            
            translated = " ".join(translated)
            
            print(translated)
            
        except ValueError:
            if word.lower() == "exit":
                word = word.lower()
                print("Adios")
            else:
                print("Try again.\n")

converter(input('Words to translate into piglatin: '))
5
  • 5
    You need to change the print(<variable>) to return <variable> in order to return values for python Commented Jan 19, 2022 at 23:34
  • 3
    What happens when you run the code? How is that different from what you want to happen? Please read How to Ask and ask a question. But in the mean time, make sure you understand that print and return have nothing to do with each other. Commented Jan 19, 2022 at 23:42
  • Hi @taxevader, unless Im misunderstanding...the code still does not run when replacing print with return - codehs.com/sandbox/id/python-3-AYiGVB?filename=main.py Commented Jan 20, 2022 at 1:27
  • @captainUmerica could you clarify more on the input you used and the expected result? Do you intend to have the result printed out on the "Output" tab Commented Jan 20, 2022 at 1:49
  • Yes @taxevader, the output should translate whatever content a user adds. Here is a working version without the try/except codehs.com/sandbox/id/python-3-g0xl5Z Commented Jan 20, 2022 at 2:10

1 Answer 1

1

The problem with the code is that it enters an infinite while loop in the converter() function.

You should move the while loop outside the function:

while True:
   converter(input('Words to translate into piglatin: '))

Then, you can process the input to check if the user wants to quit. If the user chooses not to quit, then you should pass the input string to the converter() function. You already have most of the components needed looking at your CodeHS link without the try/except block.

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

8 Comments

I believe this loop was intentional for the converter. OP just didn't understand how to return the function which would solve the infinite loop as a bonus.
@12944qwerty from what I see in their second CodeHS link, it seems that the while loop was not present in his working version. Hence my suggestion of just wrapping all that code in a while loop outside the converter() function and doing a check to see if the user wants to exit.
That is because that code works for just one word each. OP is attempting to get an entire sentence to be converted; the while loop loops through each word in this problem.
@12944qwerty Maybe I'm understanding this incorrectly, but in their second CodeHS link, their code still manages to convert multiple words/a sentence with the use of list comprehension, specifically: [piglatin(word) for word in split]
@captainUmerica Right, that means the code in your second CodeHS link i.e. the working example, is very close to what you want. You can follow my suggestion, which is to wrap everything outside the piglatin(word) function in a while True loop, and add an IF statement on the user_input variable. If user_input == "exit" then break from the while loop.
|

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.