0

After selecting 2 as my choice on the menu, when I input 1 as the option, the program prints "Thanks for using.....Goodbye" and then loops back to the menu instead of stopping. I cannot figure out what the cause is, since I tried numerous times to fix it but failed. Please advise me what to do. Thanks

code: http://pastebin.com/kc0Jk9qY

Sorry I couldn't implement the code in this comment, was becoming a hassle.

DaCruzR
  • 347
  • 2
  • 5
  • 14

3 Answers3

0

All your code if in a while n!=1: loop. Set n=1 when you want to quit and that's it. So just change your code to the following and it will stop:

   elif endex==1:
        print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
        time.sleep(1)
        error2=False
        n=1

break will only terminate the innermost loop, so need to make sure all the outer ones get terminated as well.

EDIT Changed code is:

if choice==2:
    while error2:
        try:
            endex=input("Do you want to Exit? \nInput (1)y or (2)n: ")
            if endex== 0 or endex >=3:
                print"Try again"
            if endex==2:
                print "You have chosen to run this programme again...\n"
            if endex==1:
                print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
                time.sleep(1)
                error2=False
                n=1
Aleksander Lidtke
  • 2,876
  • 4
  • 29
  • 41
  • still not working, I tried you suggestion but still doesn't break when option 1 selected and now when I select 2 the program doesn't even loop back. – DaCruzR Sep 08 '13 at 20:09
  • Option one as in `choice==1`? If so I haven't changed that one. When `endex==1` the program will keep asking you "Do you want to Exit? \nInput (1)y or (2)n: " as the `while error2` loop will still be running. What else do you want to achieve? – Aleksander Lidtke Sep 08 '13 at 20:20
  • If you want to go back to the very beginning you need to insert ` error=False; error2=False` in the `if endex==2` block to re-run the whole thing again. – Aleksander Lidtke Sep 08 '13 at 20:22
  • This is the local copy of the code I've written based on yours, see if this works for you and, should it not, what you want to change. http://pastebin.com/3gZiMBQS – Aleksander Lidtke Sep 08 '13 at 20:32
0

What you have supplied looks correct. That break will break you out of the while error2 loop. So the problem must lie after that. I expect that if choice==2 is within another loop, and you are not breaking out of that.

Edward
  • 1,000
  • 5
  • 16
0

OK, this is it. Your error2 value is uneccessary. Simply start an infinite loop and break out on valid responses. I would consider doing this in your other loops too.

if choice==2:
    while True:
        try:
            endex=input("Do you want to Exit? \nInput (1)y or (2)n: ")
            if endex== 0 or endex >=3:
                print"Try again"
            if endex==2:
                print "You have chosen to run this programme again...\n"
                break
            if endex==1:
                print "\nThank you for using RBDC Bin2Dec Converter \nGoodbye"
                time.sleep(1)
                n=1
                break
Graeme Stuart
  • 5,837
  • 2
  • 26
  • 46
  • He won't be able to stop outer loops easily from within `if choice==2` if he does the same for outer loops. And it leaves no easy control as to which loop to terminate. – Aleksander Lidtke Sep 08 '13 at 20:31