0

Hello i have this login that i am trying to make and when i get to the command screen if i want to exit out of the program i type "exit", but the problem is i have it in a double while loop and don't know how to break out of both. Here is an example of the code:

a = 0
b = 0
loginatt = 0

while a == 0:
    if loginatt == 4:
        print "Too many tires!"
        break
    password = raw_input("Password: ")
    if password == "test":
        b = 1
        while b == 1:
            command = raw_input("command: ")
            if command == "exit":
                break
    else:
        loginatt += 1
        print "error, login failed!"

this part of the code won't break out of the double while loop:

command = raw_input("command: ")
            if command == "exit":
                break
user3349465
  • 25
  • 1
  • 5
  • 1
    Using break is a poor programming practice, try using conditions that can be met to break out of loops, or some sort of "flag". – BRBT Mar 11 '14 at 17:36
  • The answers linked by @iCodez look more Pythonic to me, and if you care, are the way Python's language designer intends for this to work. (You can see related discussion by clicking through.) – Two-Bit Alchemist Mar 11 '14 at 17:38
  • @BigRabbit I cannot concur with that. `break` has its own raison d'être. I would say that abusing Exceptions to break out of nested loops might be inconvenient and not what the inventor intended, but I see no reason why to discourage the use of `break`. – Hyperboreus Mar 11 '14 at 17:40
  • @Hyperboreus yes, you are correct. I should have made my comment more clear as to what I was referring too. – BRBT Mar 11 '14 at 17:50

3 Answers3

0

break will only leave one level of looping. You could try resetting your outer loop variable:

if command == "exit":
   a = 1
   break

However, it might be better to break your code up a bit:

def access_control(correct_pass="test", attempts=4):
    """Control access to the supplied function func."""
    for _ in range(attempts):
        password = raw_input("Password: ")
        if password == correct_pass:
            get_commands()
            break
        else:
            print "Incorrect password."
    else:
        print "Too many tries."

def get_commands():
    """Take user input until they enter 'exit'."""
    while True:
        command = raw_input("Command: ")
        if command == "exit":
            break

You can now call access_control to enter the password; if correct, you will be passed on to get_commands:

>>> access_control()
Password: test
Command: exit
>>>

or

>>> access_control()
Password: foo
Incorrect password.
Password: bar
Incorrect password.
Password: baz
Incorrect password.
Password: python
Incorrect password.
Too many tries.
>>> 
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

There is no keyword to break from more than one loop. You can try the following:

wantToBreak = False
while b == 1:
    command = raw_input("command: ")
    if command == "exit":
        wantToBreak = True
        break
if wantToBreak:
        break

It utilizes a boolean variable which indicates whether the flow should be interrupted (break more levels).

Mateusz
  • 3,038
  • 4
  • 27
  • 41
0

Generally variable names like a and b aren't very explicit. Also your login functionality is independent of your command functionality, hence why not seperate both?

#! /usr/bin/python2.7

def login(maxTries = 4):
    while maxTries:
        password = raw_input('Password: ')
        if password == 'test':
            return True
        maxTries -= 1
    print 'Too many tries.'
    return False

def runCommand():
    while True:
        command = raw_input('Command: ')
        if command == 'exit': break
        print 'processing command', command

if login(): runCommand()

Instead of breaking when finding the command exit you can also use an iterator with a sentinel:

def runCommand():
    for command in iter(lambda: raw_input('Command: '), 'exit'):
        print 'processing command', command
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87