Python Forum
while loop will not stop looping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
while loop will not stop looping
#1
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 !
Reply
#2
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:
Reply
#3
Why not use more common “while True:” and “break” where appropriate?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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
Reply
#5
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.
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 901 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  get out of while loop and stop repeat Frankduc 11 2,984 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  "while" loop is looping back too early mangurian 1 1,281 Jan-28-2022, 09:15 AM
Last Post: ibreeden
  Stop/continue While loop block Moris526 68 25,509 Mar-28-2021, 09:21 PM
Last Post: Larz60+
  While loop keeps looping mcoliver88 3 2,210 Jul-29-2020, 12:48 PM
Last Post: buran
  Nested for loop not looping puttingwordstogether 0 1,715 Jun-16-2020, 11:15 PM
Last Post: puttingwordstogether
  'Looping' does not work out within a 'for Loop' Placebo 4 3,343 Sep-15-2018, 08:19 PM
Last Post: Placebo
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,344 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  For looping over a list, editing the list from inside the loop? Krookroo 3 3,949 Sep-04-2017, 05:08 PM
Last Post: Krookroo
  build a list (add_animals) using a while loop, stop adding when an empty string is en nikhilkumar 1 8,896 Jul-17-2017, 03:29 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020