PYTHON   42
askAndCountVowels
Guest on 27th August 2023 06:08:05 AM


  1. def num_vowels(s):
  2.     '''Return the number of vowels in string s.
  3.    The letter "y" is not treated as a vowel.'''
  4.                        
  5.     count = 0
  6.     for char in s:
  7.         if char in "aAeEiIoOuU":
  8.             count = count + 1
  9.                                
  10.     return count                       
  11.  
  12.  
  13. def askAndCountRepeat():
  14.     ''' Prompt the user to enter a string and find out if it
  15.        contains any vowels. Repeat this task until a string
  16.        containing a vowel is found.  When you find a string
  17.        containing a vowel, return the number of vowels in
  18.        that string (an int)'''
  19.     num = 0    
  20.     while num == 0:
  21.         myString = input("Enter a string: ")
  22.         num = num_vowels(myString)
  23.     return num
  24.  
  25. def main():
  26.     num = askAndCountRepeat()
  27.     print("Finally!  Found a string with vowels.")
  28.     print("It has",num,"vowels")
  29.  
  30. main()

Raw Paste

Login or Register to edit or fork this paste. It's free.