1

I am trying to solve this problem below

Assume s is a string of lower case characters. Write a program that counts up the number of vowels contained in the string s. Valid vowels are: 'a', 'e', 'i', 'o', and 'u'. For example, if s = 'azcbobobegghakl', your program should print:

Number of vowels: 5

I wrote the following code

s='azcbobobegghakl'
count = 0
for vowels in s:
    if vowels in 'aeiou':
        count += 1
print ('Number of vowels: ' + str(count))

This was not correct. I learned that you should not define "azcbobobegghakl" as s or g or anything else for that matter. Do I need to use a certain function to accomplish this?

7
  • Not really sure what you mean. Something like this? for vowels in 'azcbobobegghakl'? Commented Apr 1, 2018 at 1:15
  • I am not sure if the grader will type "c' and expects the count for azcbobobegghakl or will type nothing and expects the program to return something. With your suggestion, would it look like this count = 0 for vowels in "azcbobobegghakl" if vowels in 'aeiou': count += 1 print ('Number of vowels: ' + str(count)) Commented Apr 1, 2018 at 1:36
  • ignore the formatting. I don't know why it went wrong. Commented Apr 1, 2018 at 1:37
  • @magnetrtk have you tried my edited answer? I'm quite confident in that's what your problem was. Commented Apr 1, 2018 at 1:54
  • Someone pointed out that s can be anything. This makes sense now that I read the question again. Commented Apr 1, 2018 at 2:29

3 Answers 3

2

You can use a list comprehension and then count the list.

print("Number of vowels: {}".format(len([vowel for vowel in input() if vowel in "aeiou"])))

The question is asking to count the number of vowels in any string, not just the example azcbobobegghakl, therefore you should replace the fixed string with an input().

Sign up to request clarification or add additional context in comments.

8 Comments

I actually really like this answer, but I don't think it's very readable to someone new to python.
I am new to Python. I understand most of what you said, but is there a way I can adapt what I have written, rather than completely changing it? I am getting very low marks (1 mark) despite it running.
Ahh, I get your question now... s should be a dynamic input, try it now. If you're using Python 2 replace input with raw_input
an alternative not deserving a separate answer: sum(vowel in "aeiou" for vowel in input())
@Marat wow nice, but I doubt a beginner will understand much on what's going on. Though your way is a lot closer to the OP's original intent.
|
1

What you have seems to do the task required by the question, however, if the do want it in the form of a function you can restate the code you already have as a function:

def count_vowels(s):
  count = 0
  for vowels in s:
      if vowels in 'aeiou':
          count += 1
  print ('Number of vowels: ' + str(count))

Then, you can execute your program with the following:

count_vowels('azcbobobegghakl')

3 Comments

I don't know if they actually want it in the form of a function. Someone on the course board suggested to another person to try input or type(s) = str. Then another suggested to not bother with input. I read another thread that suggested to go to watch a youtube video which tells new programmers to assume the grader will not use "s=blablalba". I don't know exactly what that means in terms of what the grader will do in order to move forward.
@magnetrtk I'm not an authority on how your schools review process works, is there a support email you can contact to ask?
I looked on the board and they remove certain comments and I doubt they will give out too much info.
0
s = str(input("Enter a phrase: "))
count = 0
for vowel in s:
    if vowel in 'aeiou':
        count += 1
print("Number of vowels: " + str(count))

This seems to work on python but it is not the right answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.