1

I've only been using Python for a few months and I'm a bit stuck. This is a chunk of a longer code:

while True:

    method=input('''How would you like to analyse your data?
    1 = mean
    2 = quartile
    3 = mode
    4 = range
    5 = variance
    6 = standard deviation
    ''')

    if method == '1':

        mean=sum(theList)/len(theList)
        print('The mean of this data set is '+str(mean)+'.')

        while True:
            moveOn=input('Calculate another measure? Y/N ')
            if moveOn == 'Y' or moveOn == 'y':
                print('Redirecting...')
                time.sleep(1)
                break
            elif moveOn == 'N' or moveOn == 'n':
                print('Thank you for using the PDAP. ')
                break
            else:
                print('Invalid response. ')

The problem is, I need the elif option to break out of the first while loop, but also out of the second. If someone types 'n', I need to program to just finish there and stop completely, but I can't seem to work out how to do that.

timgeb
  • 76,762
  • 20
  • 123
  • 145
allybee_chan
  • 25
  • 1
  • 5

1 Answers1

3

You could add a variable to break the loop, kinda like this:

_run = True
while _run:
    while True:
        [... do something ...]
        _run = False
        break

Or, if you want to just quit your program, you could do this directly by calling sys.exit().

andreas-hofmann
  • 2,378
  • 11
  • 15