Bottom Page

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()
Quote
#2
When you use "return", your function stop there and returns what you specified.
Quote
#3
Thanks for your help, but when I remove the return statements, if I enter "hello there" it prints:
hello there
hello there
Quote
#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)
Quote
#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()

Quote
#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
Quote
#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()
Quote
#8
It works, Thanks guys.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to get all the possible permutation and combination of a sentence in python sodmzs 1 177 Jun-13-2019, 07:02 AM
Last Post: perfringo
  Sentence maker help bidoofis 2 308 Feb-08-2019, 03:59 AM
Last Post: bidoofis
  wont print last sentence.. mitmit293 2 245 Jan-27-2019, 05:38 PM
Last Post: aakashjha001
  Trouble with Regex Translator skrivver99 3 367 Dec-15-2018, 03:55 PM
Last Post: Gribouillis
  Output discrepancy when building Translator skrivver99 17 901 Nov-26-2018, 01:22 AM
Last Post: ichabod801
  Help with pig latin translator DragonG 1 394 Nov-01-2018, 03:57 AM
Last Post: ichabod801
  Generating all simple sentence possibilities Max_77 3 548 Oct-10-2018, 11:35 AM
Last Post: Max_77
  Python - Limit Sentence Length to 10 Words - Text file dj99 2 995 Jul-21-2018, 02:24 PM
Last Post: dj99
  Is there py code that splits an audio containing speech per every sentence? infuj02 1 707 Mar-07-2018, 04:30 PM
Last Post: Larz60+
  Pig Latin Translator RickyWilson 1 773 Dec-02-2017, 08:20 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)