-2

I'm trying to write an if statement with elif statements and then an else statement at the end, except its completely skipping over my elif statements and it executes the else statement no matter what (even the if statement).

    word = input('Enter an all lowercase word to be translated: ')

    if word[:1] == ('a', 'e', 'i', 'o', 'u'):
        print(word + 'way')
    elif word[:2] ==('a', 'e', 'i', 'o', 'u') :
        print(word[1:] + word[0] + 'ay')
    elif word[:3] == ('a', 'e', 'i', 'o', 'u'):
        print(word[2:] + word[0:] + 'ay')
    else:
        print(word)

if i delete the last elif and else statements and turn the first elif statement into an else statement, it'll run through exactly how I want it to but I need the elif statements.

    if word[1] == ('a', 'e', 'i', 'o', 'u'):
        print(word + 'way')
    else:
        word[2] ==('a', 'e', 'i', 'o', 'u')
        print(word[1:] + word[0] + 'ay')
e-bird14
  • 13
  • 1
  • 5
  • Review how slicing works. word[:2] returns the first and second character – jeff carey Jan 30 '17 at 23:58
  • you want to evaluate if the first character is a vowel, use the `in` keyword. for example: `if word[0] in ('a', 'e', 'i', 'o', 'u'): print(word+'way')` – mmenschig Jan 31 '17 at 00:00

3 Answers3

2

There are a couple issues here.

  1. Slicing a string returns all characters up to the slice index
  2. You are compared that against a tuple of vowels.

I think you just want to do array lookups for your if statements and instead of == use in (...)

EDIT: Code Example

word = input('Enter an all lowercase word to be translated: ')

vowels = ('a', 'e', 'i', 'o', 'u')

if word[1] in vowels:
  print(word + 'way')
elif word[2] in vowels:
  print(word[1:] + word[0] + 'ay')
elif word[3] in vowels:
  print(word[2:] + word[0:2] + 'ay')
else:
  print(word) 
quikst3r
  • 1,783
  • 1
  • 10
  • 15
0

You ask the user to enter a word, but you compare the slice (which is a string with one or multiple letters) to a tuple. This condition will never be true. If you want to test if a letter is contained in your tuple, you can use in:

if word[:1] in ('a', 'e', 'i', 'o', 'u'):
    print(word + 'way')

There is also a problem with your further comparisons. You slice multiple letters by using word[:2] (say word = "test", then word[:2] would be "te"). Multiple letters would also never be contained in the tuple, even if you check with "in". To slice the second letter you can write word[1:2].

Tristan
  • 1,576
  • 9
  • 12
0

The way you have it right now, the right-hand side of your conditions is a tuple value, which is never equal to your left-hand side, which is a string.

>>> vowels = ('a','e','i','o','u')
>>> type(vowels)
<class 'tuple'>
>>> vowels = 'aeiou'
>>> type(vowels)
<class 'string'>
>>> type(word[:1])
<class 'string'>

If you're trying to convert words into pig-latin, there's only two cases: whether the last character in "words" ends in a vowel or not.

You can try the following expression:

if words[-1] in 'aeiou':
    print(words[:-1]+'way')
else:
    print(words[:-1]+'ay')
jlau
  • 366
  • 1
  • 4