Python循环遍历列表但没有找到值

noo*_*014 1 python python-2.7

谢谢你提前看看这个!基本上我有一个while循环是循环在试图找到数字1我的代码的下方,它遍历"几乎"所有的列表中,但没有找到索引值1,任何见解列表?

numbers = [24, 10, 92, 28, 71, 1, 80, 70]
counter = 0
number_to_find = 1

def my_loop():
    global counter
    while counter > 6: #Also not sure while 7 doesn't work, says out of range?
        if numbers[counter] == number_to_find:
            print "Number found at position", counter 

    else:
        print "Counter not found in position" , counter
        counter = counter + 1
        my_loop()


print my_loop()
Run Code Online (Sandbox Code Playgroud)

koj*_*iro 9

这是一个非常令人困惑的代码.

  1. print的输出,但功能不返回任何东西.(它也可以打印东西,但它不会返回任何东西.)
  2. 该函数有一个while循环,也会自行重复.(在某些情况下这是合适的,但这不是其中之一.)
  3. counter最初为0,但您的第一次检查是否counter > 6.0不大于6.(这在技术上不是错误的,但非常令人困惑.)
  4. 价值1是指数5.为什么你要找6?
  5. 达到所需索引后,不会终止循环.