I am trying to write a function that returns true if the number of vowels in a word are equal to the consonants or false if otherwise, but I keep on getting an error that the word is not defined, I need help to debug my code

def word(n):
    n=n.copy
    w=list(n)
    v=['a','e','i','o','u']
    for item  in w:
        for item in v:
            while w>x:
                if w-x==0.5:
                    return 'True'
            else:
                    return 'False'
            end
        end
    end

word(deaf)

Edited 6 Years Ago by vegaseat: added code tags

Pretty wild coding with a little bit of Ruby thrown in!

Here is just one small hint using actual Python code:

def get_vowels(word):
    number_vowels = 0
    for c in word:
        if c in 'aeiouy':
            number_vowels += 1
    return number_vowels

def get_consonants(word):
    number_consonants = 0
    for c in word:
        if c not in 'aeiouy':
            number_consonants += 1
    return number_consonants

# test
word = 'deaf'
vowels = get_vowels(word)
consonants = get_consonants(word)
print("%s has %d vowels and %d consonants" % (word, vowels, consonants))

i am trying to write a python function which takes a word(as a string) and returns True if the word contains the same number of vowels as consonants but why is it giving an error

def w(word):
       n = vowels in word
       m = consonants in word
if n == m:
       return True
else:
       return False

Edited 6 Years Ago by vegaseat: added code tags

pasriah, your algorithm there looks good as long as "vowels in word" and "consonants in word" represent functions that return the number of vowels in the word and the number of consonants in the word, respectively.

bumsfeld's two functions do exactly what you want.

By the way, you'll find debugging your code much easier if you use variable names that describe what the variable is used for. "vowel_count" tells you what the number it holds means, but "n" could represent anything.

[elem for elem in v if elem ==elem in w]
v is the list of vowels
w is the list of character in the word

This article has been dead for over six months. Start a new discussion instead.