0

I want this loop to break when user enters anything but y or Y. But, instead the Enter the new location prompt is returning again and again.

    def add_locations(self):
        while True:
            new = str(input("Enter the new loation: "))
            self.locations.append(new)
            ask = str(input("add new location? (y/n):  "))
            if ask == 'y' or 'Y':
                continue
            else:
                break

enter image description here

forest
  • 1,312
  • 3
  • 20
  • 47

2 Answers2

3

Your if-clause is evaluated like this: (ask == 'y') or 'Y' and 'Y' is always true. You need to ask if ask == 'y' or ask == 'Y': instead

peer
  • 4,171
  • 8
  • 42
  • 73
1

Alternatively, you can put all your positive values into a tuple:

while True:
    new = str(input("Enter the new location: "))
    self.locations.append(new)
    ask = str(input("add new location? (y/n):  "))
    if ask.lower() in ('y', 'yes'):
        continue
    else:
        break
trsvchn
  • 8,033
  • 3
  • 23
  • 30