-2

I just started to learn programming and one of my tasks was to create a pig Latin translator in python

Heres my code:

def pyglatin():
    words = input("Enter a word please:").split()
    for word in words:
    print(word[1:] + word[0] +"ay", end = " ")      

if word[0] == "a" or "i" or "u" or "e" or "o":
    print(word[0] + word[1:] + "way", end = " ")
    

pyglatin()

My problem is that the result keeps printing both words:

Enter a word please:hello
ellohay helloway

Also, how can I make the users input all lowercase?

Thanks :)

sdrr453e
  • 41
  • 6

2 Answers2

0

There are a couple of issues here. Your indentation is off, but most importantly, your if statement will always return True. To make a comparison against many values, replace word[0] == "a" or "i" or "u" or "e" or "o" with word[0] in {"a", "i", "u", "e", "o"}, otherwise the 2nd part of your statement will always evaluate to True.

Perhaps this is what you're after:

def pyglatin():
    words = input("Enter a word please:").split()
    for word in words:
      print(word[1:] + word[0] +"ay", end = " ")      

    if word[0] in {"a", "i", "u", "e", "o"}:
        print(word[0] + word[1:] + "way", end = " ")
    
pyglatin()

Output:

Enter a word please:hello
ellohay
Enter a word please:abc
bcaay abcway

To make the input lowercase, use words = input("Enter a word please:").lower().split().

PApostol
  • 2,152
  • 2
  • 11
  • 21
0
words = input("Enter a word please:")
words=words.split()
for word in words:
   print(word[1:] + word[0] +"ay", end = " ")      
   if word[0] == "a" or word[0]=="i" or word[0]=="u" or word[0]=="e" or 
word[0]=="o":
      print(word[0] + word[1:] + "way", end = " ")