I’m new to Python. While going through an online Python tutorial about while loops, I wrote some code that results in an infinite loop and I just can’t understand why. Here’s the code:
x = 1 powers_of_two = 2**x while powers_of_two <= 1024: print(powers_of_two) x += 1
This just returns infinite 2s printed over and over. When I write the code like so, it works as expected and yields the desired returns:
x = 1 powers_of_two = 2**x while powers_of_two <= 1024: print(powers_of_two) powers_of_two *= 2
I understand why the latter version works, but I just don’t understand why the first version doesn’t work and return the exact same results. Please, any understanding you can shed on how this is working will be greatly appreciated. Thank you!