How do I check if the string cointains any list's element | TypeError: 'in <string>' requires string as left operand, not list | Beginner question
How do I check if the string cointains any list's element | TypeError: 'in <string>' requires string as left operand, not list | Beginner question
我正在尝试制作一个程序,使用两个单词的元音生成密码(密码是这些单词中元音的索引)。例如,对于 Python Snake,密码为 424:
Python Snake
4 2 4
012345 01234
下面是我笨拙的代码,我得到了“TypeError: 'in ' requires string as left operand, not list”,抱歉我太蠢了,但我想不出其他不使用的方法作为左操作数的列表。
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)
一些建议会有所帮助。
谢谢。
代码:
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)
输入:
Please input 1sr word: python
Please input 2nd word: snake
输出:
424
我正在尝试制作一个程序,使用两个单词的元音生成密码(密码是这些单词中元音的索引)。例如,对于 Python Snake,密码为 424:
Python Snake
4 2 4
012345 01234
下面是我笨拙的代码,我得到了“TypeError: 'in ' requires string as left operand, not list”,抱歉我太蠢了,但我想不出其他不使用的方法作为左操作数的列表。
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)
一些建议会有所帮助。
谢谢。
代码:
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)
输入:
Please input 1sr word: python
Please input 2nd word: snake
输出:
424