mas=[] while True: for x in range(0,100): c=x**2+156*x-2000 mas.append(c) if c>=0: print(c,x) break print(mas) |
2 answers
You have two cycles. Internal stops. External infinite. It should probably be something like this:
mas=[] doLoop = True while doLoop: for x in range(0,100): c=x**2+156*x-2000 mas.append(c) if c>=0: print(c,x) doLoop = False break print(mas) - Thanks for the answer. I just thought that the break command terminates the while loop always. - Alexander
- one@ Alexander break command interrupts only the nearest cycle, but in your case it is
for- andreymal - I would say not the closest but the cycle in which the interrupt operator is called - JVic
- Now I figured it out, thank you - Alexander
|
Why is there at all while ?
mas=[] for x in range(0,100): c=x**2+156*x-2000 mas.append(c) if c>=0: print(c,x) break print(mas) The result is the same.
You can use the generator:
new_list = [x**2+156*x-2000 for x in range(0, 100) if x**2+156*x-2000 <= 0] print(new_list) Result:
[-2000, -1843, -1684, -1523, -1360, -1195, -1028, -859, -688, -515, -340, -163]
And further. In your code, the list is:
sixteen
Judging by your condition, there should be only negative values. But since you add a value to the list before checking, it gets 16 and after that the cycle is interrupted.
Change a little code. Like this:
mas=[] for x in range(0,100): c=x**2+156*x-2000 if c>=0: print(c,x) break else: mas.append(c) print(mas) - Thanks, understood, redid. - Alexander
|
while True? - YozhEzhi