嵌套For和While语句 [英] Nested For and While Statements

查看:123
本文介绍了嵌套For和While语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用一些常量然后一个while语句启动我的代码。但是我在$语句中有一些For语句在while语句中结束了

我开始得到一些错误。我希望我不必发布我的

代码,但它看起来像这样:


导入os,string


这个时候:


所有代码都缩进它应该

最后一行在。


我在范围内(1)

class = []

class2 = []

对于范围内的i(2 )

这样做

这个

那个。


Next Line遇到障碍?该行应该在哪里?
是什么?

I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I''m hoping I won''t have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should
Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?

推荐答案

ko **** @ hotmail.com 写道:

我用一些常量开始我的代码然后是一个while语句。但是我在$语句中有一些For语句在while语句中结束了

我开始得到一些错误。我希望我不必发布我的

代码,但它看起来像这样:


导入os,string


这个:


所有代码都缩进它应该


最后一行在While。


我在范围内(1)

class = []

class2 = []

For i在范围内(2)

这样做

这个

那个。


下一行在这里遇到障碍?这行应该在哪里?

是什么?
I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I''m hoping I won''t have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should
Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?



你的for循环都使用相同的计数索引。

Your for loops both use the same counting index.


9月24日下午2:28,kou ... @ hotmail.com写道:
On Sep 24, 2:28 pm, kou...@hotmail.com wrote:

我用一些常量开始我的代码然后一段时间声明。但是我在$语句中有一些For语句在while语句中结束了

我开始得到一些错误。我希望我不必发布我的

代码,但它看起来像这样:


导入os,string


这个时候:


所有代码都应该缩小它应该


最后一行在While。


我在范围内(1)

class = []

class2 = []

For i在范围内(2)

这样做

这个

那个。


下一行在这里遇到障碍?这条线应该在哪里?

I start my code with some constants then a while statement. But I
have some For statements towards the end within the While statement
where I start getting some errors. I''m hoping I won''t have to post my
code, but it looks something like this:

Import os, string

while this:

All Code indented like it should

Last line in While.

For i in range(1)
class = []
class2 = []
For i in range(2)
Do this
And this
And that.

Next Line hits snag here? Where should this line
be?



为什么for循环比while的主体进一步缩进?如果

他们是while的一部分,For i in range(1)应排在

最后一行在while。如果他们不是时间的一部分,但是之后是b $ b,那么For i in range(1)应该与while:相对应。


另外,正如James Stroud所说,你的for循环索引是相同的

变量。 />

- Paul

Why are the for loops further indented than the body of the while? If
they are part of the while, "For i in range(1)" should line up with
"Last line in While." If they are not part of the while, but come
after, then "For i in range(1)" should line up with "while this:".

Also, as James Stroud said, your for loop indexes are the same
variable.

-- Paul


你的for循环都使用相同的计数索引。


由于这些变量是for循环的局部变量,理论上它应该使用相同的变量来处理两个循环。虽然实际上很糟糕,但我在我的机器上测试了它,下面的代码确实按照预期工作了b $ b,所以看来问题是缩进了

正确循环。

范围内的j(10):

print j,first loop

for j in范围(5):

print" ",j," 2nd loop"

Your for loops both use the same counting index.

Since those variables are local to the for loop, theoretically it
should work with both loops using the same variable. Although bad
practice, I tested it on my machine and the following code does indeed
work as expected, so it appears that the problem is indenting the for
loops properly.
for j in range( 10 ):
print j, "first loop"
for j in range( 5 ):
print " ", j, "2nd loop"


这篇关于嵌套For和While语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆