直到语句/循环python?
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_
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: '))