Home > Net >  How do I check if the string cointains any list's element | TypeError: 'in <string>&
How do I check if the string cointains any list's element | TypeError: 'in <string>&

Time:11-16

I am trying to make a program that will generate passcodes using vowels from two words (the passcode is the index of the vowels in those words). eg for Python Snake the passcode would be 424:

Python Snake
    4    2 4
012345 01234

Below is my clumsy code, I got the "TypeError: 'in ' requires string as left operand, not list", sorry for being stupid but I can't think of other way of doing this without using a list as a left operand.

vowels = ["A", "E", "I", "O", "U"]
vowels_lower = ["a", "e", "i", "o", "u"]

word1 = input("Please input 1sr word: ")
word2 = input("Please input 2nd word: ")


def passcode(FirstWord, SecondWord):
  for i in range(0, len(word1), 1):
    if vowels in word1[i]:
      return i
    elif vowels_lower in word1[i]:
      return i
    print(i)

  for i in range(0, len(word2), 1):
    if vowels in word2[i]:
      return i
    elif vowels_lower in word2[i]:
      return i
    print(i)
  
     
passcode(word1, word2)

Some advice would be helpful.

Thanks.

CodePudding user response:

Code:

vowels = ["A", "E", "I", "O", "U"]


word1 = input("Please input 1sr word: ")
word2 = input("Please input 2nd word: ")


def passcode(FirstWord, SecondWord):
    ans1=''
    ans2=''
    for idx, chr in enumerate(FirstWord):
        if chr.upper() in vowels:
            ans1 =str(idx)
    for idx, chr in enumerate(SecondWord):
        if chr.upper() in vowels:
            ans2 =str(idx)
    ans = ans1 ans2
    return ans
            
  
     
passwd=passcode(word1, word2)
print(passwd)

Input:

Please input 1sr word: python
Please input 2nd word: snake

output:

424
  • Related