基本问题 [英] Basic question

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

问题描述

我已经学习了2周的python并且在以下问题中遇到了




为范围内的j(10):

打印j

如果(真):

j = j + 2

打印''interno'', j


会发生什么是j = j + 2例如,IF内部不会像在C或Java中那样改变循环

计数器(j)。


我错过了什么?


[]'s

Cesar

I''ve been studying python for 2 weeks now and got stucked in the
following problem:

for j in range(10):
print j
if(True):
j=j+2
print ''interno'',j

What happens is that "j=j+2" inside IF does not change the loop
counter ("j") as it would in C or Java, for example.

Am I missing something?

[]''s
Cesar

推荐答案

Cesar G. Miguel写道:
Cesar G. Miguel wrote:

for j in range(10):

print j

if (真):

j = j + 2

打印''interno'',j


会发生什么?当J = J + 2英寸例如,IF内部不会像在C或Java中那样改变循环

计数器(j)。

我错过了什么?
for j in range(10):
print j
if(True):
j=j+2
print ''interno'',j

What happens is that "j=j+2" inside IF does not change the loop
counter ("j") as it would in C or Java, for example.
Am I missing something?



如果你想要那种行为,那就使用`while`结构:


j = 0

而j < 5:

打印j

如果为真:

j = j + 3

print'' - '' ,j $ />

如果你使用for循环,每次通过foor循环时,Python

按顺序将下一项分配给`j`变量。


HTH,

Karlo。

If you want that kind of behaviour then use a `while` construct:

j = 0
while j < 5:
print j
if True:
j = j + 3
print ''-- '', j

If you use a for loop, for each pass through the foor loop Python
assigns next item in sequence to the `j` variable.

HTH,
Karlo.


5月12日下午5:18,Cesar G. Miguel < cesar.go ... @ gmail.comwrote:
On May 12, 5:18 pm, "Cesar G. Miguel" <cesar.go...@gmail.comwrote:

我现在已经学习了2周的python并且被困在了

以下问题:


范围内的j(10):

print j

if(True):

j = j + 2

打印''interno'',j


会发生什么是j = j + 2 ;例如,IF内部不会像在C或Java中那样改变循环

计数器(j)。


我错过了什么?
I''ve been studying python for 2 weeks now and got stucked in the
following problem:

for j in range(10):
print j
if(True):
j=j+2
print ''interno'',j

What happens is that "j=j+2" inside IF does not change the loop
counter ("j") as it would in C or Java, for example.

Am I missing something?



是的你是:)


" j in range(10):..."意思是:

1.建立一个清单[0,1,2,3,5,5,7,8,9]

2.对于这个元素列表(0,然后1,然后2,...),将j设置为

值然后执行循环体内的代码


模拟&for;(< initialisation>;< condition>;< increment>)< body>"

你必须在Python中使用:


< initialisation>

而< condition>:

< body>

< increment>


当然,在大多数情况下,它不会是pythonic。做的方式

吧:)


-

Arnaud

Yes you are :)

"for j in range(10):..." means:
1. Build a list [0,1,2,3,4,5,6,7,8,9]
2. For element in this list (0, then 1, then 2,...), set j to that
value then execute the code inside the loop body

To simulate "for(<initialisation>; <condition>; <increment>) <body>"
you have to use while in Python:

<initialisation>
while <condition>:
<body>
<increment>

Of course in most case it would not be the "pythonic" way of doing
it :)

--
Arnaud


Cesar G. Miguel写道:
Cesar G. Miguel wrote:

我已经学习了2周的python并且被困在了

以下问题:


for j in range(10):

print j

if(True):

j = j + 2

打印''interno'',j


会发生什么是j = j + 2例如,IF内部不会像在C或Java中那样改变循环

计数器(j)。


我错过了什么?


[]'s

Cesar

I''ve been studying python for 2 weeks now and got stucked in the
following problem:

for j in range(10):
print j
if(True):
j=j+2
print ''interno'',j

What happens is that "j=j+2" inside IF does not change the loop
counter ("j") as it would in C or Java, for example.

Am I missing something?

[]''s
Cesar



Nope。循环计数器将通过范围(10)生成的

整数列表连续分配。在循环内部,如果你改变j,那么

从该点开始穿过身体,j将具有

值。但是这样的动作不会改变下一步传递

通过循环的事实,j将被分配列表中的下一个值。

Nope. The loop counter will be assigned successively through the list of
integers produced by range(10). Inside the loop, if you change j, then
from that point on for that pass through the body, j will have that
value. But such an action will not change the fact that next pass
through the loop, j will be assigned the next value in the list.


这篇关于基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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