直到语句/循环python?

直到语句/循环python?,python,python-2.7,Python,Python 2.7,python中是否有until语句或循环?这不起作用: x = 10 list = [] until x = 0: list.append(raw_input('Enter a word: ')) x-=1 当x1!=x2循环 因此,您的代码变成: x = 10 lst = [] #Note: do not use list as a variable name, it shadows the built-in while x != 0: lst.append(raw_

python中是否有
until
语句或循环?这不起作用:

x = 10
list = []
until x = 0:
    list.append(raw_input('Enter a word: '))
    x-=1

当x1!=x2循环

因此,您的代码变成:

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

这将一直运行到x==0

除非您正在使用该变量执行某些操作,否则实际上不需要计算循环次数。相反,您可以使用最多触发10次的
for
循环:

li = []
for x in range(10):
    li.append(raw_input('Enter a word: '))
另一方面,不要使用
list
作为变量名,因为这会掩盖实际的
list
方法。

Python模拟直到使用惯用语循环:

  • 为了演示的目的,我被迫构造了一个微不足道的lambda;正如@Makoto所建议的,这里一个简单的
    range()
    就足够了
另请参见:和
li = []
for x in range(10):
    li.append(raw_input('Enter a word: '))
x = 10
list = []
for x in iter(lambda: x-1, 0):
    list.append(raw_input('Enter a word: '))