I ran into a something I did not expect when making a small program. I'm not including the whole program, just exemplifying the stick in the wheel, as it's part of a homework assignment. I'd just like some clarification on why the last "x = 1" is ignored.

x = 1

while x == 1:
	print(x)
	x = 2
while x == 2:
	print(x)
	x = 1

It looks to me that I should be getting a loop of 1, 2, 1, 2, 1, 2, 1, 2... But I only get 1, 2.

On the other hand, if I change the code to

x = 1

while x == 1:
	print(x)
	x = 2
while x == 2:
	print(x)
	x = 2         # 1 changed to 2

I get an endless loop of 1, 2, 2, 2, 2, 2, 2, 2, 2...

I've just started programming and I must say I'm a bit puzzled.

Recommended Answers

All 7 Replies

Maybe because the second loop is not nested in the first one. Nest the second one in the first one. I've never programmed in Python, but this is similar to what happens in C++ :)
Let me walk you through what is happening.
First, x is declared as 1. The loop says, while x is one, print x and change its value to 2. So it prints x, changes the value to 2, and because the loop condition is false, it exits the loop to the next one. The next one says, while x is 2, print x and change value to 1. It does print, and changes the value back to 1. Again, the loop condition is false (2 != 1) and it exits the program ;-)

Member Avatar for masterofpuppets

I agree with pspwxp fan :)
A simple fix would be to nest the second loop in the first one like this:

x = 1
while x == 1:
    print( x )
    x = 2
    while x == 2:
        print( x )
        x = 1

Heheh, i could say i'm good at Python even though this is the first time i'm seeing its code! :D

Of course! For some weird reason I assumed that the while checks were somehow global. Thanks for your help :$

Heheh, i could say i'm good at Python even though this is the first time i'm seeing its code! :D

I keep telling folks that C++ and Python have much in common, by design the Python syntax is much easier on the eye.

If you just want to toggle between two values, you could have used a tuple swap too ...

x, y = 1, 2
while True:
    print(x)
    print(y)
    y, x = x, y  # swap x and y
Member Avatar for masterofpuppets

I keep telling folks that C++ and Python have much in common, by design the Python syntax is much easier on the eye.

If you just want to toggle between two values, you could have used a tuple swap too ...

x, y = 1, 2
while True:
    print(x)
    print(y)
    y, x = x, y  # swap x and y

definitely simpler than mine dude :) :)

I keep telling folks that C++ and Python have much in common, by design the Python syntax is much easier on the eye.

Aww, man, I like C++ more, and i think its easier to the eye :)
Although that's probably because i'm used to C++ for 1 and a half years and i just saw Python right now, lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.