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)

It's not even April first yet!

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
Member Avatar for Mouche

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.