1

I'm trying out Python 3 and been doing a practice with a while loop but the loop doesn't end when the variable is set to another value different from the default 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? ");
vaultah
  • 44,105
  • 12
  • 114
  • 143
userDepth
  • 173
  • 1
  • 1
  • 8

1 Answers1

0

The or operator in your test condition is not working as you intend it to. Try this:

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? ")
juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Robbie
  • 4,672
  • 1
  • 19
  • 24
  • Thank you for the suggestion @juanpa.arrivillaga you are correct. – Robbie Nov 14 '17 at 00:10
  • could you add a little explanation? like, "The or operator doesn't take multiple variables..." – userDepth Nov 14 '17 at 00:21
  • I took the liberty of removing semi-colons from the end of your statements. While syntactically valid, they are discouraged, unless required (i.e. two seperate statements on the same line, e.g. `my_list.append(x); my_list.append(y)` – juanpa.arrivillaga Nov 14 '17 at 00:22
  • Thanks for that, I just used the question's code as a template. – Robbie Nov 14 '17 at 00:23
  • 1
    @userDepth the problem is, something like `op is 'y' or 'Y'` evaluates to `op is ('y' or 'Y')` and `'y' or 'Y'` evaluates to `'Y'`. This is explained in great detail in the duplicates – juanpa.arrivillaga Nov 14 '17 at 00:23