为什么while循环返回误报?

时间:2017-11-13 23:50:40

标签: python while-loop

我正在尝试Python 3并且正在使用while循环进行练习但是当变量设置为与默认Y不同的另一个值时,循环不会结束

#!/usr/bin/python3

elements = [];
copy = [];
rot=0;
op='y';
ap='y';

while op is 'y' or 'Y':

    elements = [];
    a = int(input("How many elements? "));
    for i in range(a):
        elements.append(input("Enter element " + str(i+1) + " "));

    while ap is 'y' or 'Y':
        rot = int(input("How many element to rotate? "));
        print(elements);
        copy = elements[-rot:] + elements[:-rot];
        elements = copy;
        print(elements);
        ap = input("Rotate again? ");

    op = input("Create a new rotatrix? ");

1 个答案:

答案 0 :(得分:0)

您的测试条件中的运算符无法正常工作。试试这个:

elements = []
copy = []
rot=0
op='y'
ap='y'

while op == 'y' or op == 'Y':

    elements = []
    a = input("How many elements? ")
    for i in range(a):
        elements.append(input("Enter element " + str(i+1) + " "))

    while ap == 'y' or ap == 'Y':
        rot = input("How many element to rotate? ")
        print(elements)
        copy = elements[-rot:] + elements[:-rot]
        elements = copy
        print(elements)
        ap = raw_input("Rotate again? ")

    op = raw_input("Create a new rotatrix? ")