0

I am working on a program that changes the user's inputted word into Pig Latin.

def main():
    word = input('Please enter a word ')
    if word[0] == "A" or "E" or "I" or "O" or "U":
        print(word + "HAY")
    else:
        print(word + word[0] + "HAY")
      
main()

I have a certain set of rules to follow: if the user's inputted word has a, e, i, o,or u as its starting index, the program prints the word with HAY printed at the end of it. If the word has any other letter at the beginning, the program is supposed to take the first letter of the word and place it at the front of HAY.

That's where my issue is. I can't quite figure out what I am supposed to type in order to have the program remove that first index and place it just behind where HAY prints.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Try `print(word[1:] + word[0] + "HAY")` to skip the first letter – Mike67 Nov 06 '20 at 22:20
  • 1
    Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Random Davis Nov 06 '20 at 22:21
  • Let me quickly elaborate on why your initial if statement doesn't work: `or` in Python compares boolean values, so if you wanted to do the statement your way you would have to do `if word[0] == "A" or word[0] == "E" or`...etc. You can't just do `"A" or "E"` because `or` doesn't work with strings. – M-Chen-3 Nov 06 '20 at 23:07

1 Answers1

1

First, let me note that your initial if statement is flawed and always executes the if and never the else. So I changed it. I believe this is what you're trying to do:

def main():
    word = input('Please enter a word ')
    if word[0] in "AEIOU":
        print(word + "hay")
    else:
        print(word[1].upper() + word[2:] + word[0].lower() + "ay")

main()

This uses string slicing, where string[a:b] goes from string[a] up to but not including string[b], and when I leave out b it just goes to the end of the word.

M-Chen-3
  • 2,036
  • 5
  • 13
  • 34