1

I am trying to make a program that detects how many vowels are in a word you type. Here's my source code (I have multiple codes):

a = input("word - ").lower()
for i in range(1, len(a)+1):
  if a[str(i)] == "a" or "e" or "i" or "o" or "u":
    print("ok")
else:
  print("no")`

And I get the error:

TypeError: string indices must be integers

The second one:

a = input("word - ").lower()
for letter in a:
  if letter == "a" or "e" or "i" or "o" or "u":
    value = 0
    value = value + 1
print(value)

Also gives me an error:

TypeError: string indices must be integers

The third one is a little bit more complex:

a = input("rec - ").lower()
for i in range(1, len(a)+1):
  if a[str(i)] == "a":
    print("yes a")
  elif a[str(i)] == "e":
    print("yes e")
  elif a[str(i)] == "i":
    print("yes i")
  elif a[str(i)] == "o":
    print("yes o")
  elif a[str(i)] == "u":
    print("yes u")

I am working on Python 3.6.1 on Repl.it

You can check out the full source code at my profile.

I appreciate your help. Thank you!

ONT
  • 19
  • 5
  • 2
    Unrelated but still: [your ORs are all wrong](https://stackoverflow.com/q/15112125/2564301). Fixing your current error will have this one pop up. – Jongware Dec 29 '18 at 08:15
  • 1
    This part causes your error: `a[str(i)]`. See? It's a string where an index should appear. – Jongware Dec 29 '18 at 08:16

6 Answers6

3

In the first and last example you used a string as an index (a[str(i)]). However, indeces are always integers. And remember that the first index is always 0, not 1. Your for-loop iterates from 1. Since the first element has an index of 0, the last has an index of len(array) - 1, meaning, that your for-loop should only iterate to len(a). The problem with the for-loop index also applies to the last example.

In the second example you didn't use the or-statements correctly. You can't compare them like that. You would have to write it like this:

if letter == "a" or letter == "e" or letter == "i" or letter == "o" or letter == "u":

To shorten this, just write:

if letter in "aeiou":

Here, you check whether the letter is in the string "aeiou".

In your second example you also reset value to zero everytime a vowel is found. This will lead to value being only either 1, or not defined. Put value = 0 before the for-loop and it should work.

DWuest
  • 619
  • 1
  • 7
  • 18
1

Note that you need to use integers when indexing an array or string:

a = input("word - ").lower()
for i in range(len(a)):
    if a[i] == "a" or "e" or "i" or "o" or "u":
        print("ok")
else:
    print("no")

And this should solve your problem:

vowels = set("aeiou")
user_input = input("word - ").lower()
vowels_count = len([letter for letter in user_input if letter in vowels])

print(f"you typed {vowels_count} vowel(s)")
Vitor SRG
  • 664
  • 8
  • 9
  • Thank you! This solved all of my problems. The first one did not work but the second one did. Happy New Year! – ONT Dec 30 '18 at 08:33
  • I'm glad i could help. Please, if you found a solution, select one of the answers as correct. This would help others with similar doubts – Vitor SRG Dec 30 '18 at 10:30
1

you used a[str(i)] to access the letters in the string where str(i) is a string and all the array indices must be integers, that's why the error.

i is already an integer so you should be using a[i] to access letters.

also as @usr2564301 said, your or statements are wrong

if letter == "a" or "e" or "i" or "o" or "u":

should be,

if letter in ["a", "e", "i", "o", "u"]:
Auxilus
  • 217
  • 2
  • 9
0

You probably want to use the in keyword for this.

value = 0
for letter in a:
  if letter in ['a','e','i','o','u']:
    value = value + 1
print("Number of values: %d" % value)

As far as the type error. Like someone else pointed out, you need to use the integer:

a = input("word - ").lower()
for i in range(1, len(a)+1):
  if a[i] == "a" or a[i] == "e" or a[i] == "i" or a[i] == "u":
    print("ok")
  else:
    print("no")`
drGrove
  • 166
  • 12
0

You can sipmly fo:

a = input("word - ").lower()
vowels = "aeiou"
len([letter for letter in a if letter in vowels])
Sergey Pugach
  • 5,561
  • 1
  • 16
  • 31
0
vowels=['a','e','i','o','u']
count=0
a = input("word - ").lower()
for i in range(0, len(a)): #or simply range(len(a))
  if a[i] in vowels:
    count+=1
print("Number of vowels:"+str(count))
  1. string indices must be integers a[i] not a[str(i)]
  2. or statements cannot be used like that (maybe use in?)
  3. The for range is wrong .index starts from 0 not 1, so it should be range(0, len(a)) simply range(len(a))
Bitto
  • 7,937
  • 1
  • 16
  • 38