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
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
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
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.
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: '))
range()
would suffice, as as @Makoto suggested.