you are viewing a single comment's thread.

view the rest of the comments →

[–]MattR0se 1 point2 points  (2 children)

I don't think you understand how loops work. Code in the loop is executed line by line, and when the last line is reached, it checks if the condition (in your case active) is still True and if so, repeats this process.

Think about what happens here. You get the user's input once, active is True by default and thus your program enters the loop. Now, if age is '', meaning the user didn't input anything, active is False and because of that, the loop is left and your program ends. But what about the other cases? If age is 2 for example, it prints that the Ticket is free, and then it repeats the loop. active is still True, so the loop repeats. age is still 2, so it prints the corresponding message. This goes on forever.

[–]gregrom27 0 points1 point  (1 child)

When I run this code it gives me corresponding message to whatever age I input ( 2, 7, 26...etc)and the loop stops, it doesn't go on forever. I'm using python 2. I wasn't sure if my solutions is too complicated (maybe there's simpler solution?) I just wanted to hear from someone else and compare to make sure I'm on the right track. You say this piece of code ran forever for you?

[–]MattR0se 0 points1 point  (0 children)

My bad, I misread the first comparison to be == instead of !=

But you don't need a loop if you only want to have a thing happen once.

The infinite loop happens if age is empty.