Python 异常捕获偶数值为真?

Python 异常捕获偶数值为真?,python,exception,iteration,python-3.3,Python,Exception,Iteration,Python 3.3,我目前正试图编写一段代码,在序列(x)中迭代,搜索用户输入的单词 下面是代码 x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] i = -1 while True: s = input("Enter a word to search: ") if s != "Quit": try: while i < len(x): i = x.index(s, i+1)

我目前正试图编写一段代码,在序列(x)中迭代,搜索用户输入的单词

下面是代码

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
i = -1
while True:
    s = input("Enter a word to search: ")
    if s != "Quit":
        try:
            while i < len(x):
                i = x.index(s, i+1)
                print("found at index", i)
        except ValueError:
            print("Not found")
        i = -1
    else:
        break
print("Goodbye")
您正在尝试查找所有事件,但找不到超过最后一个事件的下一个事件:

>>> 'abc'.index('a', 0)
0
>>> 'abc'.index('a', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
如果只需要找到一个匹配项,即第一个匹配项,则根本不需要使用循环:

try: 
    print("found at index", x.index(s))
except ValueError:
    print("not found")
因为不需要在起始位置上循环。

您正在尝试查找所有引用,但无法找到超过最后一个引用的下一个引用:

>>> 'abc'.index('a', 0)
0
>>> 'abc'.index('a', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: substring not found
如果只需要找到一个匹配项,即第一个匹配项,则根本不需要使用循环:

try: 
    print("found at index", x.index(s))
except ValueError:
    print("not found")

因为不需要在起始位置上循环。

您总是得到
ValueError
的原因是,您总是在列表中的项目中进行迭代,直到得到
ValueError
。您需要在内部
while
循环中包含某种条件,以便它在找到元素时退出。更好的是,做我在下面发布的编辑

与此相反:

try:
    while i < len(x):
        i = x.index(s, i+1)
        print("found at index", i)
except ValueError:
    print("Not found")
i = -1

更简单。

您总是得到
ValueError
的原因是您总是在列表中的项目中进行迭代,直到得到
ValueError
。您需要在内部
while
循环中包含某种条件,以便它在找到元素时退出。更好的是,做我在下面发布的编辑

与此相反:

try:
    while i < len(x):
        i = x.index(s, i+1)
        print("found at index", i)
except ValueError:
    print("Not found")
i = -1

更简单。

如果希望列表中有多个引用的位置,请先获取计数,然后获取引用的索引

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
n = 0
while True:
    s = input("Enter a word to search: ")
    if s != "Quit":
        n = x.count(s)
        if s == 0:
            print('not found')
        else:
            i = -1
            while n > 0:
                print('found at index:', x.index(s,i+1))
                n = n - 1
    else:
        break
print("Goodbye")

不过,可能还有一种更简单的方法。

如果您希望列表中多个引用的位置,请先获取一个计数,然后获取引用的索引

x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
n = 0
while True:
    s = input("Enter a word to search: ")
    if s != "Quit":
        n = x.count(s)
        if s == 0:
            print('not found')
        else:
            i = -1
            while n > 0:
                print('found at index:', x.index(s,i+1))
                n = n - 1
    else:
        break
print("Goodbye")

不过可能还有更简单的方法。

谢谢!这个答案很完美,很有魅力。虽然我也通过将
而I
更改为
而I+1
并在
打印后添加
I+=1
(“在索引中找到”,I)
)来使用原始代码。实际上,第一种方法是完美的,但第二种方法为所有搜索生成-1索引的结果。哪种方法?
found
标志、使用
enumerate()
的列表理解或直接的
.index()
进行第一次匹配?谢谢!这个答案很完美,很有魅力。虽然我也通过将
而I
更改为
而I+1
并在
打印后添加
I+=1
(“在索引中找到”,I)
)来使用原始代码。实际上,第一种方法是完美的,但第二种方法为所有搜索生成-1索引的结果。哪种方法?
found
标志、使用
enumerate()
的列表理解,还是仅针对第一个匹配的简单的
.index()
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
n = 0
while True:
    s = input("Enter a word to search: ")
    if s != "Quit":
        n = x.count(s)
        if s == 0:
            print('not found')
        else:
            i = -1
            while n > 0:
                print('found at index:', x.index(s,i+1))
                n = n - 1
    else:
        break
print("Goodbye")