本文介绍了基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经学习了2周的python并且在以下问题中遇到了 : 为范围内的j(10): 打印j 如果(真): j = j + 2 打印''interno'', j 会发生什么是j = j + 2例如,IF内部不会像在C或Java中那样改变循环 计数器(j)。 我错过了什么? []'s CesarI''ve been studying python for 2 weeks now and got stucked in thefollowing problem:for j in range(10):print jif(True):j=j+2print ''interno'',jWhat happens is that "j=j+2" inside IF does not change the loopcounter ("j") as it would in C or Java, for example.Am I missing something?[]''sCesar推荐答案 如果你想要那种行为,那就使用`while`结构: j = 0 而j 打印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 = 0while j < 5:print jif True:j = j + 3print ''-- '', jIf you use a for loop, for each pass through the foor loop Pythonassigns next item in sequence to the `j` variable.HTH,Karlo. 是的你是:) " 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。做的方式 吧:) - ArnaudYes 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 thatvalue then execute the code inside the loop bodyTo 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 doingit :)--Arnaud Nope。循环计数器将通过范围(10)生成的 整数列表连续分配。在循环内部,如果你改变j,那么 从该点开始穿过身体,j将具有 值。但是这样的动作不会改变下一步传递 通过循环的事实,j将被分配列表中的下一个值。Nope. The loop counter will be assigned successively through the list ofintegers produced by range(10). Inside the loop, if you change j, thenfrom that point on for that pass through the body, j will have thatvalue. But such an action will not change the fact that next passthrough the loop, j will be assigned the next value in the list. 这篇关于基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!