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
1

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

Comments

1
while x != 0:
    #do stuff

This will run until x == 0

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.