Python Forum

Full Version: while loop will not stop looping
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good day!

I have been developing a text-based calculator in Python 3.x and ran into this problem, my while loop will not stop even though I changed the variable value. Confused

Here is a snippet:
done = 0
def true():
    done = 1
while done == 0:
    print('''Please select one:
1 - Equilateral triangle
2 - Right angle triangle
3 - Acute triangle
4 - Obtuse triangle
5 - Square
6 - Rectangle
7 - See more''')
    choice = input()
    if choice == 7:
        print('''
    8 - Parallelogram
    9 - Rhombus
    10 - Trapezium
    11 - Circle
    12 - Semicircle
    13 - Circular sector
    14 - Ring
    15 - Ellipse''')
    elif choice == 1:
        true()
        equ_triangle()
When I run it, it just keeps saying the original text, 1-Triangle, 2-etc etc etc.

Thanks a bunch !
the function true() (HORRIBLE NAME) does not set the global variable done, it creates a local variable that is also named done but is not related to the global done at all. It is the same as if you wrote:
done = 0

def true():
    finished = 1

while done == 0:
Why not use more common “while True:” and “break” where appropriate?
(Apr-01-2020, 04:20 PM)perfringo Wrote: [ -> ]Why not use more common “while True:” and “break” where appropriate?

I tried that, but it did not work, it still just keeps looping. Huh
In the snippet of code provided, replacing the true() call with break will exit out of the loop. If this is inside some other loop you would also have to break out of the outer loop.
Thank you everyone for your help!

I eventually did what was recommended, to use the break statement.

My problem was both my terrible true() statement (instead of break), and also my if statements were looking for int, while my input() function wasn't int(input()), it was input() so it was a string.

Thank you all!