r/codehs • • Jul 12 '22
Python help with 7.5.5
here is my code
word = input("enter word:")
vowel = ["a", "e", "i", "o", "u"]
def contains_vowel():
if vowel in word:
print("True")
else:
print("False")
contains_vowel()when I run it, I get this message:
enter word:hi
Traceback (most recent call last):
File "scratchpad.py", line 10, in <module>
contains_vowel()
File "scratchpad.py", line 5, in contains_vowel
if vowel in word:
TypeError: 'in <string>' requires string as left operand, not listanyone know how to fix this or just the correct way to do it
0
Upvotes
2
u/Traditional-Yam-2115 Jul 12 '22
So it’s been a while since I did python but it seems like it’s complaining that vowel is an array instead of a string. You would loop through all the letters in you input using for loop then see if that char is a vowel ex: for char in word: if char in ‘aeiou’ Return true
1
u/SomeElaborateCelery Jul 12 '22
So you almost got it.
contains_vowel(“hi”)
So you need to understand that functions take inputs. word is an input used in the function contains_vowel. So you need to put it in the function definition ‘def contains_vowel(word)’.
Otherwise it will use the global variable ‘word’ you defined above. Which is not a string and will give you an error.
Otherwise you were pretty close.
1
u/OSnoFobia Jul 13 '22
Vowel is an array, you cannot search an entire array in a string. You should use keywords like "any" or "all".
Just change the if statement to this:
v in word returns a boolean, true or false. If you add for v in vowel, then this will create a boolean array with every value in vowel array. It will look like [True, False, True, True, False] or something.
Then if you "any" this boolean array, "any" will return True if there is at least one true in array.
If you "all" this boolean array, "all" will return True only if all the elements are True.
1
u/Wallabanjo Jul 13 '22 edited Jul 13 '22
Learn regex.
import reword = input("enter word:")x = re.findall('[aeiou]', word)if (len(x) > 0):print("TRUE")else:print("FALSE")x will contain the vowels that were found. len(x) will tell you the length of the array x. if x > 0, then 1 or more vowels were found. Note, this is lower case only. With regex it is easy to make it a cassless search, tell you what position the vowels were found, and a heap of other things. Regex is your friend. Reddit stripped out the formatting ... but it's easy enough to figure out.
Truth be told - since you want to print out true or false, you could remove the if test entirely and just have a single line:
print((len(x)>0))Since len(x)>0 returns True or False, just printing out the result would suffice.
Actually, thinking about it further ...
import reword = input("enter word:")print(len(re.findall('[aeiou]', word)) > 0)This does everything you are asking. You really don't need the print either, but sometimes if you are running things in a notebook it doesn't display the output, so this forces it.
2
u/Ascraft325 Aug 10 '22
Here is my code and it works
def contains_vowel(letters):
--->for vowel in "aeiou":
--->--->if vowel in letters:
--->--->--->return True
--->return False
replace the ---> with indents
1
u/monadyne Jul 13 '22
syzygy
cwm
0
u/[deleted] Jul 12 '22
[deleted] — view deleted comment