Vowels and Consonants detector - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Vowels and Consonants detector (/thread-5177.html) |
Vowels and Consonants detector - OmarSinno - Sep-21-2017 Objective is: to print True when it's a vowel, and False when it's a consonant, WITHOUT using if conditions. vowels = ['o', 'i', 'y', 'e', 'a','u'] consonants =['b, c, d, f, g, h, j, k, l, m ,n ,p, q, r, s, t, v, w, x, z'] def is_vowel(c): for character in vowels: print(True) for character in consonants: print(False) is_vowel('banana')Here's what I got when I executed the program: True True True True True True FalseThis is obviously false because I should get False True False True False True. Any help? RE: Vowels and Consonants detector - buran - Sep-21-2017 execute vowels = ['o', 'i', 'y', 'e', 'a','u'] consonants =['b, c, d, f, g, h, j, k, l, m ,n ,p, q, r, s, t, v, w, x, z'] def is_vowel(c): for character in vowels: print(character, True) for character in consonants: print(character, False) is_vowel('banana')this will help to see what your script ACTUALLY does (Sep-21-2017, 09:57 AM)OmarSinno Wrote: Objective is: to print True when it's a vowel, and False when it's a consonant, WITHOUT using if conditions. RE: Vowels and Consonants detector - Sagar - Sep-21-2017 You are not using "c" which is passed into the function is_vowel() RE: Vowels and Consonants detector - j.crater - Sep-21-2017 You have an argument "c" that you pass to the function is_vowel, but where/how do you use it? Also, compare how you have defined vowels and consonants lists (or what they contain rather). RE: Vowels and Consonants detector - OmarSinno - Sep-21-2017 Isn't c the string that is supposed to be called later on in the code? def is_vowel(c): for character in c: print(character, True) for character in c: print(character, False)I adjusted it to this code, still giving me the same results. I have realized that when it's finding a vowel, it is printing True 6 times, which is the length of the string 'banana'. Same thing goes for when it finds a consonant. Unlike this code: def is_vowel2(c): for character in c: #The Character in the word defined by the letter "c" if character in vowels: #If this character is in the list "vowels" print(True) #The statement is then a vowel, which is true! else: #If not, or if else print(False) #The statement is false, then it is a consonant!Fortunately, I have to do it both ways (using if conditionals and without using them) so I can actually study both. Help? RE: Vowels and Consonants detector - buran - Sep-21-2017 vowels = ['o', 'i', 'y', 'e', 'a','u'] def is_vowel2(my_word): for character in my_word: #The Character in the word defined by the letter "c" print (character in vowels) #If this character is in the list "vowels" print True, else will print False is_vowel('banana')or vowels = ['o', 'i', 'y', 'e', 'a','u'] def is_vowel(c): return c in vowels #If this character is in the list "vowels" print True, else will print False for character in 'banana': print(is_vowel2(character)) |