0
def check_v_c(word):
    for i in word:
        if i in "AEIOUaeiou":
            return i

        else:
            i in "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"


print(check_v_c("Money"))

I was trying to loop each letter using the for loop.

petezurich
  • 9,280
  • 9
  • 43
  • 57

3 Answers3

2

I hope, this will work for your solution use string class to check for punctuation

import string
def check_v_c(word):
    result = []
    for i in word:
        if i.lower() in "aeiou":
            result.append(f'{i} is a vowel')
        elif i in string.punctuation:
            result.append(f'{i} is a punctuation')
        else:
            result.append(f'{i} is a consonant')
    return result
print('\n'.join(check_v_c("Mon.ey")))
Muhammad Ali
  • 444
  • 7
  • 2
    You could use `for i in word.lower(): if i in "aeiou"` so you don't have to apply the `lower()` method in each loop. – Jamiu S. Feb 03 '23 at 05:45
1

You only need to remove else as if if is False the it will run for loop again

def check_v_c(word):
  for i in word:
    if i in "AEIOUaeiou":
        return i


print(check_v_c("Money"))

But if you want to pass something then you can

def check_v_c(word):
  for i in word:
    if i in "AEIOUaeiou":
        return i

    else:
        "It is not vowel"


print(check_v_c("Money"))
  • def check_v_c(word): for i in word: if i in "AEIOUaeiou": print(i, " is vowel") else: print(i, "is consonant") print(check_v_c("Money")) It returns None at the end. Can youtell me why? Thanks – Hamza Khatib Feb 05 '23 at 02:33
  • @HamzaKhatib Yeh because you r trying to print the `function` (i.e., `check_v_c()`) now as it doesn't return any thing so it is returning `None` – Adarsh Gupta Feb 05 '23 at 06:42
  • @HamzaKhatib **Try this code :** `def check_v_c(word): for i in word: if i in "AEIOUaeiou": print(f"{i} is vowel") else: print(f"{i} is consonant") check_v_c("Money")` – Adarsh Gupta Feb 05 '23 at 06:43
0
  • you need to change return i to print(f"{i} is vowel") which will print something like that (o is vowel)

  • and if the first condition is False the letter will be consonant so you need to change i in "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz" to print(f"{i} is consonant") which will print something like that (m is consonant)

  • if you don't understand the format i use to print, i suggest you to check this

  • To optimize the code: use word.lower() which will reduce the number of comparisons.

def check_v_c(word):
    # Make a for loop and convert the input to lowercase to reduce the number of comparisons
    for i in word.lower():
        if i in "aeiou":
            print(f"{i} is vowel")
        else:
            print(f"{i} is consonant")

print(check_v_c("Money"))

you can see output here

Shady Emad
  • 56
  • 1
  • 5