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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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:
1
2
newsent = (" ".join(my_new_words_list))
print(newsent)
Reply
#5
Im not sure if I did it right but its still not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 8 3,186 Dec-03-2024, 10:21 AM
Last Post: skylartyler
  while sentence kimyyya 3 3,777 Mar-20-2021, 06:00 AM
Last Post: Pedroski55
  i want to write symbole as varibales in python to make translator rachidel07 9 5,023 Feb-03-2021, 09:57 PM
Last Post: nilamo
  List / arrays putting in sentence Kurta 3 3,308 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 7,788 Dec-22-2020, 07:30 AM
Last Post: martabassof
  How to match partial sentence in long sentence Mekala 1 1,999 Jul-22-2020, 02:21 PM
Last Post: perfringo
  letter translator (or someting) Obsilion 4 3,333 Apr-28-2020, 12:40 PM
Last Post: deanhystad
  Remove a sentence if it contains a word. lokhtar 6 8,243 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,729 Jan-08-2020, 08:02 AM
Last Post: perfringo
  Regex Help for clubbing similar sentence segments regstuff 3 2,869 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