-1

I have a homework assignment to create this function. Here is my code:

def countVowelsOdd(n):
    length = len(n)
    count = 0
    for i in range(length): 
        if i % 2 != 0:
            if n[i] == "a" or "A" or "e" or "E" or "i" or "I" or "o" or "O" or "u" or "U":
                count = count + 1
    return count

Here is the output vs the expected output for this function: https://i.stack.imgur.com/R419V.png

quant
  • 2,184
  • 2
  • 19
  • 29
Steve
  • 31
  • 7
  • 1
    // , What research have you done already? Could you put your output in a text format, rather than an image that we might not be able to copy? – Nathan Basanese Nov 05 '18 at 21:53

2 Answers2

0

If you need to account for vowels in odd positions do:

for i in range(1, length, 2):

Check range docs where it says that you can pass start, stop and step arguments. Then range will go from 1, 3, 5, ..., to length-1.

You could also do

if n[i].lower() in "aeiou":

As a shorter expression for checking if letter is vowel. Check this question as an example.

Cheche
  • 1,456
  • 10
  • 27
  • (1) `range` will not go until `length`, the stop parameter is *exclusive*, (2) `x in ['aeiou']` will only be true if `x` is exactly the string `'aeiou'`, not if `x` is one of the characters a, e, i, o, u. – mkrieger1 Nov 05 '18 at 21:22
  • Thanks for your comment. You're right. Just edited my answer. – Cheche Nov 05 '18 at 21:27
0

Actually your only problem is the line

if n[i] == "a" or "A" or "e" or "E" or "i" or "I" or "o" or "O" or "u" or "U":

it either needs to be

if n[i] == "a" or n[i] == "A" or n[i] == "e" or n[i] == "E" or n[i] == "i" or n[i] == "I" or n[i] == "o" or n[i] == "O" or n[i] == "u" or n[i] == "U":

or you can also use

if n[i] in "aAeEiIoOuU":

which I actually prefer. However this line can still get improved

if n[i].upper() in "AEIOU":
quant
  • 2,184
  • 2
  • 19
  • 29