0

I'm trying to make code that allows me to take an input string and return how many vowels are in that input string. I had it working with built in functions, but my teacher asked me to then convert it to a user defined function and I've been having a ton of problems. I managed to fix most of them, and have spent several hours trying to search for the answer but I don't know enough terminology yet to search well.

I've tried(and have been fiddling with it for a couple of hours now but no luck.):

def count_vowels(string):
    string = input('Enter string: ')

def count_vowels(i):
    return i in string
    if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' ):
        vowels = vowels + 1

print(count_vowels(string))

Current error is 'i' is undefined.

Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
Alden Tank
  • 31
  • 3
  • can you post the full error as the code you hav given when run prints true dosnt give error i is undefined – Chris Doyle Oct 04 '19 at 10:26
  • `return sum([item.lower() in 'aeiou' for item in i])`. You've already got answers to your problem, that gives you the next step in your learning: _list comprehensions_ and how booleans are a subclass of integers, so can be summed – roganjosh Oct 04 '19 at 10:40

4 Answers4

0

UPDATE

First of all you use the input function inside your count_vowels so you do not pass string as parameter. Try the following:

def count_vowels():
    string = input('Enter string: ')
    vowels = 0
    for i in string:
        if i.lower() in 'aieou':
            vowels += 1
    return vowels

print(count_vowels())

could also be done in one line:

print(len([x for x in input('Enter string: ') if x.lower() in 'aieou']))
Community
  • 1
  • 1
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
  • `for i in string: if i.lower() in ('a', 'e', 'i', 'o', 'u'):`? – roganjosh Oct 04 '19 at 10:31
  • Thank you so much, now I know what works I'll spend some time working out why it works like that. Thanks dude. – Alden Tank Oct 04 '19 at 10:32
  • @roganjosh yeap of course. I was a bit lazy to change it entirely so copied his part of `if` statement. :) – Kostas Charitidis Oct 04 '19 at 10:32
  • @AldenTank Updated with roganjosh 's observation – Kostas Charitidis Oct 04 '19 at 10:33
  • Wait, I missed the main problem here. There is no need to have the `input()` inside the function _at all_. There was nothing wrong with how the OP was doing it; it's in fact more correct than your approach. The only side issue is that `string` is a module too, so there could be a name collision, but it would be rare – roganjosh Oct 04 '19 at 10:47
  • @roganjosh of course there is no need but it's not a problem too. I said about the `input` because he used a `string` parameter in his function and then inside he instantiated the `string` with `input()` which is redundant and wrong. – Kostas Charitidis Oct 04 '19 at 10:49
  • No they didn't. There is no call to `input()` in the body of that function. There is a broken `return`, but no `input` – roganjosh Oct 04 '19 at 10:51
  • Ok. We're going to disagree about having `input` inside the function -> I don't think it belongs there and it makes the function less extensible. For 1-liners, I already posted one as a comment under the question – roganjosh Oct 04 '19 at 10:56
  • @roganjosh i didn't see your comment. Also the extensibility is very objective issue while we're talking about a very specific example with very specific needs but ok! :) – Kostas Charitidis Oct 04 '19 at 10:59
0

You should read an String parameter as input, i named it input_string, then character by character i've checked whether it's a vowel or not, i'm counting it using variable vowels, finally i return it as an answer

def count_vowels(input_string):
    vowels = 0
    for i in input_string:
    if(i=='a' or i=='e' or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' ):
        vowels = vowels + 1
    return vowels

string = input('Enter string: ')
print(count_vowels(string))
Mohsen_Fatemi
  • 2,183
  • 2
  • 16
  • 25
0

A quick and clear solution is the following:

string = input('Enter string: ')

def count_vowels(i):
  counter = 0
  for letter in i:
    if letter.lower() in ['a', 'e', 'i', 'o', 'u']:
      counter += 1
  return counter

print(count_vowels(string))

The lower() function transforms each letter to undercase, so it can be compared more easily.

Please don't hesitate to ask if you have any question about it.

Regards

carlesgg97
  • 4,184
  • 1
  • 8
  • 24
  • a string is in its self a list so instead of `['a', 'e', 'i', 'o', 'u']` you can just have `'aeiou'` – Chris Doyle Oct 04 '19 at 10:34
  • 1
    @ChrisDoyle it's not a list, it's a sequence. And for longer sequences, it might make sense to make it a `set()` – roganjosh Oct 04 '19 at 10:35
  • 1
    A string is not a list - but rather an iterable. So yes, you are in fact right. ['a', 'e', 'i', 'o', 'u'] could easily be substituted by 'aeiou'. – carlesgg97 Oct 04 '19 at 10:35
  • yes sorry you are right list was a poor choice of word as you said its an iterable – Chris Doyle Oct 04 '19 at 10:37
0

You can use the fact that a string is actually an iterable of chars and get rid of all your explicit i == 'a' or i == 'A' stuff and instead just write a string of the letters you want to match and check if i is in it.

def count_vowels(string):
    vowels = 0
    for i in string:
        if i in 'AEIOUaeiou':
            vowels += 1
    return vowels


string = input('Enter string: ')
print(count_vowels(string))

INPUT

The quick brown fox jumps over the lazy dog

OUTPUT

11
Kostas Charitidis
  • 2,991
  • 1
  • 12
  • 23
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42