while循环内部循环

时间:2014-02-21 09:44:52

标签: python loops for-loop while-loop

我正在尝试执行使用两个嵌套循环的Python脚本 第一个是for和第二个(嵌套的是while)。

这是我的代码:

for currentData in data:
    currentState = ''
    count = 0
    while currentState == '': 
        currentState = someMetdhodExecution() ...
        count++

当我将while放入for脚本崩溃时。

请问,请帮我解决这个问题。

提前谢谢!

2 个答案:

答案 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