如何解决ValueError:不在列表中

时间:2020-03-18 23:43:34

标签: python python-3.x list

虽然实际上在列表中,但我在这里遇到同样的问题。可能还有另一个错误,是我的代码检查它,请:

base = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
c= base.index(a)

a是输入,我确保它是“ o”,“ k”

ValueError: ['o', 'k'] is not in list

此外,我不能放try:,因为必须完成这一行,或者整个程序无法正常工作

2 个答案:

答案 0 :(得分:0)

您可以像这样尝试给定输入中的每个字母, 输入:

ip = ["o", "k"]
c = (base.index(i) for i in ip)

输出:

for i in c:
...     print(i)
...
14
10

答案 1 :(得分:0)

“基本”似乎不是列表列表。

代替:

if ['o','k'] in list:

您应该使用:

if 'o' in list and 'k' in list: