Home python How to get out of two nested cycles

How to get out of two nested cycles

Author

Date

Category

Looking for a cycle of the current computer user on the local network

def scan_lan ():
  IP_List = ['192.168.4.']
  I = 50.
  While i & lt; = 240:
    Scan_ip = IP_List [0] + STR (I)
    response = subprocess.popen (["Ping", "-n", "1", "-w", "200", scan_ip]). Wait ()
    if (response == 0):
      Try:
        WQL = 'SELECT * from WIN32_COMPUTERSYSTEM'
        C = WMI.WMI (SCAN_IP, User = 'Office \ Admin', Password = 'fff')
        For Item In C.Query (WQL):
          CURRENTUSER = STR (ITEM.USERNAME)
          Print (CurrentUSER)
          if (currentuser == "Ivanov"):
            Print (SCAN_IP)
            Break
      Except:
          Print ("-")
    I + = 1
  PRINT ("END")
Scan_lan ()

Everything works well, but I want to go out completely from the While cycle, how to do it? Put here

print (scan_ip)
 Break

But it apparently comes out only from for, and how to make a global break?


Answer 1, Authority 100%

Break stops only the nested cycle. The external cycle continues to work. Use RETURN when the result is found in your case:

if current_user == 'ivanov':
  Return Scan_ip.

Answer 2, AUTHORITY 60%

can be done so

while i & lt; = 240:
    Scan_ip = IP_List [0] + STR (I)
    response = subprocess.popen (["Ping", "-n", "1", "-w", "200", scan_ip]). Wait ()
    if (response == 0):
      Try:
        WQL = 'SELECT * from WIN32_COMPUTERSYSTEM'
        C = WMI.WMI (SCAN_IP, User = 'Office \ Admin', Password = 'fff')
        For Item In C.Query (WQL):
          CURRENTUSER = STR (ITEM.USERNAME)
          Print (CurrentUSER)
          if (currentuser == "Ivanov"):
            Print (SCAN_IP)
            Break
        ELSE:
         I + = 1 # It will go here if the cycle ended completely (that is, it was not found)
         PRINT ("END")
         Continue;
      Except:
          Print ("-")
    Break; # Follow and interrupt a big cycle here if an object was found in the internal

Here is a simpler example for understanding

for x in x in xrange (10):
  For y in XRANGE (10):
    For z in XRANGE (10):
      Print X, Y, Z
      IF x * y * z == 30:
        Break # interrupt the inner cycle
    ELSE:
      Continue # continue if the inner cycle was not interrupted
    Break # Inner cycle was interrupted, interrupt and this cycle
  ELSE:
    Continue.
  Break

Answer 3, Authority 40%

Use the flag (Boolean value) to exit cycles. When entering the CurrentUser == “Ivanov” condition, change its value to a positive value, make Break, and in the main loop, set the WHILE I & LT condition; = 240 and not result, then the cycle will be completed as soon as Result becomes truth.

Global Break, to exit all cycles, does not exist.


Answer 4

A:

list_ = [1, 2, 3, 4, 5]
Check = True.
While Check:
  FOR V IN LIST_:
    IF v == 3:
      Check = false
      Break
    ELSE:
      Print (V)

B:

list_ = [1, 2, 3, 4]
Try:
  While True:
    FOR V IN LIST_:
      IF v == 3:
        Raise Userwarning
      ELSE:
        Print (V)
Except Userwarning: Pass

1

2

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions