Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8621839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:54:30+00:00 2026-06-12T06:54:30+00:00

I have a homework question which asks to read a string through raw input

  • 0

I have a homework question which asks to read a string through raw input and count how many vowels are in the string. This is what I have so far but I have encountered a problem:

def vowels():
    vowels = ["a","e","i","o","u"]
    count = 0
    string = raw_input ("Enter a string: ")
    for i in range(0, len(string)):
        if string[i] == vowels[i]:
            count = count+1
    print count

vowels()

It counts the vowels fine, but due to if string[i] == vowels[i]:, it will only count one vowel once as i keeps increasing in the range. How can I change this code to check the inputted string for vowels without encountering this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:54:31+00:00Added an answer on June 12, 2026 at 6:54 am

    in operator

    You probably want to use the in operator instead of the == operator – the in operator lets you check to see if a particular item is in a sequence/set.

    1 in [1,2,3] # True
    1 in [2,3,4] # False
    'a' in ['a','e','i','o','u'] # True
    'a' in 'aeiou' # Also True
    

    Some other comments:

    Sets

    The in operator is most efficient when used with a set, which is a data type specifically designed to be quick for “is item X part of this set of items” kind of operations.*

    vowels = set(['a','e','i','o','u'])
    

    *dicts are also efficient with in, which checks to see if a key exists in the dict.

    Iterating on strings

    A string is a sequence type in Python, which means that you don’t need to go to all of the effort of getting the length and then using indices – you can just iterate over the string and you’ll get each character in turn:

    E.g.:

    for character in my_string:
        if character in vowels:
            # ...
    

    Initializing a set with a string

    Above, you may have noticed that creating a set with pre-set values (at least in Python 2.x) involves using a list. This is because the set() type constructor takes a sequence of items. You may also notice that in the previous section, I mentioned that strings are sequences in Python – sequences of characters.

    What this means is that if you want a set of characters, you can actually just pass a string of those characters to the set() constructor – you don’t need to have a list one single-character strings. In other words, the following two lines are equivalent:

    set_from_string = set('aeiou')
    set_from_list = set(['a','e','i','o','u'])
    

    Neat, huh? 🙂 Do note, however, that this can also bite you if you’re trying to make a set of strings, rather than a set of characters. For instance, the following two lines are not the same:

    set_with_one_string = set(['cat'])
    set_with_three_characters = set('cat')
    

    The former is a set with one element:

    'cat' in set_with_one_string # True
    'c' in set_with_one_string # False
    

    Whereas the latter is a set with three elements (each one a character):

    'c' in set_with_three_characters` # True
    'cat' in set_with_three_characters # False
    

    Case sensitivity

    Comparing characters is case sensitive. 'a' == 'A' is False, as is 'A' in 'aeiou'. To get around this, you can transform your input to match the case of what you’re comparing against:

    lowercase_string = input_string.lower()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a homework question which asks what the four functions of the Transport
I have a homework that asks this: Make an algorithm and pseudocode, which will
I have a question about my C++ homework. I am just confused about *this.
A recent homework assignment I have received asks us to take expressions which could
As question states, I am doing homework which have 2 variables BOOK, and MAGAZINE
Preface: This is not a homework question. I'm going through an algos book in
I have in my homework some question about data structure: I have elements which
This is a homework question. I have to write a program forking itself 20
I have a linked list question that stems from this homework prompt I am
This is my first question, and yes, it is a homework assignment. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.