3

Is there an until statement or loop in python? This does not work:

x = 10
list = []
until x = 0:
    list.append(raw_input('Enter a word: '))
    x-=1
  • See also: [Is there a “do … until” in Python?](http://stackoverflow.com/questions/1662161/is-there-a-do-until-in-python) and [Repeat-Until loop in python or equivalent](http://stackoverflow.com/questions/16758807/repeat-until-loop-in-python-or-equivalent) – John1024 Feb 10 '15 at 04:03

4 Answers4

1

The equivalent is a while x1 != x2 loop.

Thus, your code becomes:

x = 10
lst = [] #Note: do not use list as a variable name, it shadows the built-in
while x != 0:
    lst.append(raw_input('Enter a word: '))
    x-=1
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
1
while x != 0:
    #do stuff

This will run until x == 0

James Lemieux
  • 720
  • 1
  • 9
  • 26
1

You don't really need to count how many times you're looping unless you're doing something with that variable. Instead, you can use a for loop that will fire up to 10 times instead:

li = []
for x in range(10):
    li.append(raw_input('Enter a word: '))

As an aside, don't use list as a variable name, as that masks the actual list method.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Python emulates until loops with the iter(iterable, sentinel) idiom:

x = 10
list = []
for x in iter(lambda: x-1, 0):
    list.append(raw_input('Enter a word: '))
  • I was forced to construct a trivial lambda for demonstration purposes; here a simple range() would suffice, as as @Makoto suggested.
ankostis
  • 8,579
  • 3
  • 47
  • 61