-1
def has_a_vowel(a_str):
    for letter in a_str:
        if letter in "aeiou":
            return True
        else:
            return False
    print("Done!")

Calling this function would only check the first element... How can I get it to run through the string before returning True or False? Thank you

rpanai
  • 12,515
  • 2
  • 42
  • 64

1 Answers1

4

It would be esiest to remove the else: return False and return False outside the loop:

def has_a_vowel(a_str):
    for letter in a_str:
        if letter in "aeiou":
            return True    # this leaves the function

    print("Done!")     # this prints only if no aeiou is in the string
    return False       # this leaves the function only after the full string was checked

or simpler:

def has_a_vowel(a_str): 
    return any(x in "aeiou" for x in a_str)

(which would not print Done though).

Readup:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69