-2

I am working on an assignment for my summer school class and a question that asks us to do this has me stumped. Please help!

Below is an image of the question:

and here is my attempt at the question:

def canReleaseHounds(s):

vowel = ('aeiouAEIOU')
index = 0
while index < len(s):
    index = s.find(vowel, index)
    if index == -1:
        break
    print ('Vowel found at ' + index)
    index += 1

print (canReleaseHounds('thats not nice'))

1 Answers1

1

You can try this:

def canReleaseHounds(sentence):
    vowels = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]

    new_sentence = sentence.split()

   return any([True if i[0] in vowels or i[1] in vowels else False for i in new_sentence]

if canReleaseHounds(sentence):
     print("He can be fired")

else:
    print("He cannot be fired")

The python any() function checks if only one element is a "Truthy" statement, which means it is true, in a python sense. In this case, the for loop loops through the words in the sentence, and if the first or second word is a vowel, it will store True in the list. If both the first and the second letters in the word are not found in the vowel list, then the list stores False. When the list is passed to the any() function, it will look for any instance of True. If no True is found in the list, then it will return False. However, if True is in the list, it will return True. In your example, True will be stored if there is only one occurence of a vowel in the first two letters. That lone true will be enough to return True, meaning that Lenny can be fired, because he needs to speak only one vowel first to get himself in "trouble". I hope this helps!

More on the Python any and all functions:

How do Python's any and all functions work?

Community
  • 1
  • 1
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Thank you so much! it works fine but I do not really understand the very last part of your code regarding when the if statement returns a false boolean. Could you explain under what condition it returns false? – Keanu Nasrala May 20 '17 at 15:59
  • That cleared it up for me, thank you kindly for your help!! – Keanu Nasrala May 20 '17 at 16:08
  • You are welcome. Please check this answer as accepted, so other users know it has been answered. Thank you! – Ajax1234 May 20 '17 at 16:09