我正在尝试执行使用两个嵌套循环的Python脚本 第一个是for和第二个(嵌套的是while)。
这是我的代码:
for currentData in data:
currentState = ''
count = 0
while currentState == '':
currentState = someMetdhodExecution() ...
count++
当我将while
放入for
脚本崩溃时。
请问,请帮我解决这个问题。
提前谢谢!
答案 0 :(得分:3)
在while
循环中设置for
循环没有错。
i = 0
kebab = ["chicken","garlic","cheese","tomato","lettuce","chilli"]
print "Kebabs are so good, this is what mine has:"
excitement_over_kebab = 1
for ingredients in kebab:
while excitement_over_kebab == 1:
print kebab[i]
i+=1
if i == 6:
print "Enough talk, my lunch break is over."
excitement_over_kebab = 0
Kebabs are so good, this is what mine has:
chicken
garlic
cheese
tomato
lettuce
chilli
Enough talk, my lunch break is over.
答案 1 :(得分:1)
你的程序没有崩溃,它应该给你一个SyntaxError
(至少,它不是一个执行错误)。 Python中不存在语法count++
。您应该使用count += 1
:
for currentData in data:
currentState = ''
count = 0
while currentState == '':
currentState = someMethodExecution() ...
count += 1
# ^^^^ note the modification here