Python Forum
Two loops behaving differently - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Two loops behaving differently (/thread-31663.html)



Two loops behaving differently - DavidTheGrockle - Dec-26-2020

One of the options below prints "a = 1" as the first term and the other doesn't. WHY ?

for a in range(1, 4, 1): # This option prints a = 1 as first term in results
#for a in range(1, 50, 10): # This option does not print a = 1
print(a = ", a)
b = 0
while b < 5:
z = b*4
z += 1
print(" b = ",b)


RE: Two loops behaving differently - MK_CodingSpace - Dec-26-2020

Both print a = 1 on my machine


RE: Two loops behaving differently - deanhystad - Dec-26-2020

Please post the code that causes the problem. The code you provided does not run (error in the first print statement, while b < 5 loop never ends). When I fixed the error I could not make the code exhibit the behavior you describe.

I'm sure this code is similar to your code, but the most important part is missing. The part that demonstrates your problem. You cut that part out because you didn't think it mattered. Obviously it does. Please post the actual code that has the actual problem.


RE: Two loops behaving differently - DavidTheGrockle - Dec-26-2020

(Dec-26-2020, 08:05 PM)deanhystad Wrote: Please post the code that causes the problem. The code you provided does not run (error in the first print statement, while b < 5 loop never ends). When I fixed the error I could not make the code exhibit the behavior you describe.

I'm sure this code is similar to your code, but the most important part is missing. The part that demonstrates your problem. You cut that part out because you didn't think it mattered. Obviously it does. Please post the actual code that has the actual problem.



RE: Two loops behaving differently - DavidTheGrockle - Dec-26-2020

I'm sorry. Of course the last line should be b += 1 not z += 1. Also, for some reason in my post I could not get the code to indent. I'm sure you twigged this.

I ran the two variants on a Google Colab notebook.

The two variants appear logically identical to me and if you list the ranges there seems no reason why a = 1 should not be printed in both cases.


RE: Two loops behaving differently - deanhystad - Dec-27-2020

In the posed code both variants print a = 1. I do not think the code you posted accurately represents the code you are trying to debug. Please post the code that is not printing a = 1. Configure the code to not print = 1. Run the code on your notebook to verify it does not print a = 1. Copy the exact and post the code without any further edits.

When you post the code use python tags (use the button provided in the editor window) to retain indentation.