Maker Pro
Maker Pro

Define a Function - brutishly

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
LOL - My homework was completed, however, I think it was by force rather than skill:

L4 Problem 10
(5/5 points)
Define a function isVowel(char) that returns True if char is a vowel ('a', 'e', 'i', 'o', or 'u'), and False otherwise. You can assume that char is a single letter of any case (ie, 'A' and 'a' are both valid).

Do not use the keyword in. Your function should take in a single string and return a boolean.

Code:
def isVowel(char):
  '''
  char: a single letter of any case

  returns: True if char is a vowel and False otherwise.
  '''
  if char == 'a' or char == 'e' or char == 'i' or char == 'o' or char == 'u' \
            or char == 'A' or char == 'E' or char == 'I' or char == 'O'\
            or char == 'U':
       isVowel = True   
  else:
       isVowel = False
  return isVowel



Is there a cleaner way of doing this?
 

Supercap2F

Mar 22, 2014
550
Joined
Mar 22, 2014
Messages
550
How about converting the letter to upper case (I assume that there's a way of doing that in python?) and then you only have to test for the upper case vowels.
Dan
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
How about converting the letter to upper case (I assume that there's a way of doing that in python?) and then you only have to test for the upper case vowels.
Dan
Thanks Dan, LOL - the very same idea was presented in the next lesson! :p:D
# string.upper()

If I created a string to use for comparison, I could have then simply converted the case and retested. I am not sure if the test would be for each element in the string though as I had in the above code. I needed to capture each occurrence of the vowel, regardless of case.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Code:
def isVowel(char):
  '''
  char: a single letter of any case

  returns: True if char is a vowel and False otherwise.
  '''
  isVowel = False
  for letter in "aeiou"
    if string.lower(char) == letter
      isVowel = True  
  return isVowel

Does this work?
 

chopnhack

Apr 28, 2014
1,576
Joined
Apr 28, 2014
Messages
1,576
Code:
def isVowel(char):
  '''
  char: a single letter of any case

  returns: True if char is a vowel and False otherwise.
  '''
  isVowel = False
  for letter in "aeiou"
    if string.lower(char) == letter
      isVowel = True 
  return isVowel

Does this work?
It did not, but the automatic grader works off the principal that it will supply the input for (char). I am not sure what the error was exactly, but it pointed to the "aeiou" - does that line need a : at the end?

I guess then you could convert the contents of variable letter to upper case and test again.
 

Gryd3

Jun 25, 2014
4,098
Joined
Jun 25, 2014
Messages
4,098
Code:
def isVowel(char):
  '''
  char: a single letter of any case

  returns: True if char is a vowel and False otherwise.
  '''
  isVowel = False
  for letter in "aeiou":
    if string.lower(char) == letter:
      isVowel = True 
  return isVowel

Does this work?
It did not, but the automatic grader works off the principal that it will supply the input for (char). I am not sure what the error was exactly, but it pointed to the "aeiou" - does that line need a : at the end?

I guess then you could convert the contents of variable letter to upper case and test again.
I missed the ':' on the For loop, and IF statement... I'm not used to those things...
(Fixed)
 
Top