Python Forum
PigLatin Sentence Translator Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PigLatin Sentence Translator Help
#1
What I want me code to do is be able to take a sentence from a user input and output each word in that sentence in Pig Latin form. If I enter "hello there" it says ellohay, it is only printing the first word in pig latin. I am also having trouble with running code in different applications and stuff like that. It won't work in BBEdit or Terminal, but it will in JupterLab. So I would also appreciate it if any could recommend some software for a Mac because I think the software I'm using might be the problem.
def pig():
	
	sentence = input("Enter a sentence: ")
	
	
	for word in sentence.split():
	
		first_letter = word[0]
	
		if first_letter in "aeiou":
		
			word = word + "ay"
			return word
			
		else:
		
			word = word[1:] + first_letter + "ay"
			return word

		newsent = (" ".join(sentence.split()))
		print(newsent)
	

pig()
Reply
#2
When you use "return", your function stop there and returns what you specified.
Reply
#3
Thanks for your help, but when I remove the return statements, if I enter "hello there" it prints:
hello there
hello there
Reply
#4
You have to store the modified "word" in a variable inside the for loop (maybe a list).

Then, this part must be outside the for loop:
        newsent = (" ".join(my_new_words_list))
        print(newsent)
Reply
#5
Im not sure if I did it right but its still not working.

def pig():
	
	sentence = input("Enter a sentence: ")
	
	
	for word in sentence.split():
	
		first_letter = word[0]
		
		newlist = []
	
		if first_letter in "aeiou":
		
			word = word + "ay"
			
			newlist.append(word)
		else:
		
			word = word[1:] + first_letter + "ay"
			
			newlist.append(word)

	newsent = (" ".join(newlist))
	print(newsent)
	

pig()
Reply
#6
The following code will help you get started, however you have two remaining tasks:
a. Punctuation
b. Combining the new words into a sentence
def pig():
     
    sentence = input("Enter a sentence: ")
     
     
    for word in sentence.split():
     
        first_letter = word[0]
        print(word)
     
        if first_letter in "aeiou":
         
            word = word + "ay"
          
             
        else:
         
            word = word[1:] + first_letter + "ay"
      
        print(word)
        print("")
 
    newsent = (" ".join(sentence.split()))
    print(newsent)
     
 
pig()
print("done")
Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply
#7
You are resetting the newlist every loop iteration.
Check this:

def pig():

    sentence = input("Enter a sentence: ")
    newlist = []

    for word in sentence.split():
        first_letter = word[0]

        if first_letter in "aeiou":
            word = word + "ay"
        else:
            word = word[1:] + first_letter + "ay"

        newlist.append(word)

    newsent = (" ".join(newlist))
    print(newsent)


pig()
Reply
#8
It works, Thanks guys.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  (python) Can i get some help fixing a English to Morse translator? Pls AlexPython 7 1,539 Sep-12-2022, 02:55 AM
Last Post: AlexPython
  while sentence kimyyya 3 2,907 Mar-20-2021, 06:00 AM
Last Post: Pedroski55
  i want to write symbole as varibales in python to make translator rachidel07 9 3,410 Feb-03-2021, 09:57 PM
Last Post: nilamo
  List / arrays putting in sentence Kurta 3 2,515 Dec-25-2020, 11:29 AM
Last Post: Larz60+
  How to make a telegram bot respond to the specific word in a sentence? Metodolog 2 6,271 Dec-22-2020, 07:30 AM
Last Post: martabassof
  How to match partial sentence in long sentence Mekala 1 1,486 Jul-22-2020, 02:21 PM
Last Post: perfringo
  letter translator (or someting) Obsilion 4 2,420 Apr-28-2020, 12:40 PM
Last Post: deanhystad
  Remove a sentence if it contains a word. lokhtar 6 5,773 Feb-11-2020, 04:43 PM
Last Post: stullis
  I want to filter out words with one letter in a string (pig latin translator) po0te 1 2,065 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Regex Help for clubbing similar sentence segments regstuff 3 2,113 Nov-20-2019, 06:46 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020