为什么我的If / Else循环会脱离我的For循环?

时间:2019-05-30 12:57:03

标签: python python-3.x

我正在做一个据说很简单的python挑战,一个朋友给了我,涉及一个电梯及其移动背后的逻辑。一切都进展顺利,直到我不得不写出如何确定电梯是否可以在到达下一个排队楼层的途中撞到被叫楼层的地方为止。

def floorCompare(currentFloor,destinationFloor,calledFloor):
    if calledFloor > currentFloor and calledFloor < destinationFloor:
        return(True)
    elif calledFloor < currentFloor and calledFloor > destinationFloor:
        return(True)
    else:
        return(False)

floor = "1"
doors = "closed"
queue = []
def elevator(): # function defines how the elevator should move
    print("The Elevator is on floor: 1. The doors are "+doors+".")
    for x in range(int(input("How many floors need to be visited? "))):
        callFloor = int(input("Floor to call the elevator to: "))
        queue.append(callFloor)
        if callFloor > 10 or callFloor < 1:
            raise Exception(str(callFloor)+" is not a valid floor.")
    if queue[0] == 1:
        del queue[0]
        print("The elevator has arrived on floor 1, and the doors are open.")
    print("The queue of floors to visit is...",queue)
    for x in queue:
        print("The elevator's doors are closed and it's moving to floor:",x)
        floor = str(x)
        print("...")
        print()
        print("The elevator has arrived on floor "+floor+", and the doors are open.")
        queue.remove(x)
        addFloor = int(input("Floor to call the elevator to: "))
        if addFloor > 10 or addFloor < 1:
            raise Exception(str(addFloor)+" is not a valid floor.")
        print(queue)
        if floorCompare(int(floor), int(queue[0]), int(addFloor)) == True:
            print("The elevator can hit this stop en route to its next one.")
        else:
            print("The elevator must hit this stop separately.")
    print("Continuing Queue")

elevator()

因此在For循环中,有一个嵌套的If / Else循环,我认为它将与for循环中的其余代码一起进行迭代。但是,当我运行代码时,到达If / Else循环时,它脱离了For循环并以其快乐的方式继续进行,而忽略了数组中需要执行的任何其他迭代。这是怎么回事?

当我使用一组基本的试用版代码运行代码时,这就是我得到的输出。

The Elevator is on floor: 1. The doors are closed.
How many floors need to be visited? 4
Floor to call the elevator to: 3
Floor to call the elevator to: 6
Floor to call the elevator to: 9
Floor to call the elevator to: 10
The queue of floors to visit is... [3, 6, 9, 10]
The elevator's doors are closed and it's moving to floor: 3
...

The elevator has arrived on floor 3, and the doors are open.
Floor to call the elevator to: 7
[6, 9, 10]
The elevator must hit this stop seperately.
The elevator's doors are closed and it's moving to floor: 9
...

The elevator has arrived on floor 9, and the doors are open.
Floor to call the elevator to: 3
[6, 10]
The elevator must hit this stop separately.

Process finished with exit code 0

3 个答案:

答案 0 :(得分:3)

提早退出的原因是因为您正在循环浏览列表时对其进行了修改。举一个简单的例子:

l = [3,6,9,10]

for x in l:
    print(x)
    l.remove(x)

输出为

3
9

但是,您当前的代码还有很多其他问题。我可以捕捉到的是:

  1. 您不会在for循环中添加新调用的楼层。
  2. 正在使用floorCompare作为目的地调用queue[0]方法,但这不是 final 方法。如您所见,由于您比较了73,因此6在途中不被考虑。您应该比较3和最远的10

还请注意,queue最初并未排序,并且中间调用也将不按顺序进行。因此,在使用它时,请记住这一点。

答案 1 :(得分:0)

我认为您的问题在于将for循环与queue.remove()函数结合使用。在列表运行时编辑列表时,for x in queue:运算符似乎遇到了问题。我建议改用while queue:并将x设置为第一个元素。

    while queue:
        x = queue[0]
        print("The elevator's doors are closed and it's moving to floor:",x)
        floor = str(x)
        print("...")
        print()
        print("The elevator has arrived on floor "+floor+", and the doors are open.")
        queue.remove(x)
        addFloor = int(input("Floor to call the elevator to: "))
        if addFloor > 10 or addFloor < 1:
            raise Exception(str(addFloor)+" is not a valid floor.")
        print(queue)
        if floorCompare(int(floor), int(queue[0]), int(addFloor)) == True:
            print("The elevator can hit this stop en route to its next one.")
        else:
            print("The elevator must hit this stop separately.")

答案 2 :(得分:0)

之所以跳过第6层,是因为要从列表中删除正在迭代的数据。

l=[3,6,9,10,14]
for i in l:
    print(i)

输出: 3 6 9 10 14

for i in l:
    print(i)
    l.remove(i)

输出: 3 9 14