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 list

anyone know how to fix this or just the correct way to do it

0 Upvotes

23 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Jul 13 '22

what does

for w in word:
    if w in vowel:

I feel like I should know this

1

u/DismalDark3953 Jul 13 '22

The first line is initializing a loop to run some code for each character in the String word. For each iteration of the loop, the value will be represented by w. So the “if w in vowel” will check if the current character of the loop is in your vowel array.

1

u/SomeElaborateCelery Jul 13 '22

Basically w is the first letter in the word you parse into it, during the first iteration. So parsing “hi”, w is h the first time you go through the loop and ‘i’ the second time.

1

u/[deleted] Jul 13 '22

I think I got it, when I ran it, it worked... but it printed the answer out alot with false in it for some reason