Python function does not return as expected

I am trying to create a simple function to start a while loop and add the entered IP addresses to the list for other purposes. What I can see with my print statements, I just add to the list variable that IP recently entered, and the last listing of the list returns an empty list.

def IP_Range():
    while True:
        ipLIST = []
        IP = raw_input('Please enter IP or Network, enter "end" to break: ')
        if IP == 'end':
            break
        else:
            ipLIST.append(IP)
            print ipLIST
    print ipLIST

IP_Range()

Thanks in advance, I know that it is really simple, and I am missing something obvious. As you can tell, I am new to Python and to programming in general.

+4
source share
4 answers

, , . ipLIST = [] , reset .

def IP_Range():
    ipLIST = []
    while True:
        IP = raw_input('Please enter IP or Network, enter "end" to break: ')
        if IP == 'end':
            break
        else:
            ipLIST.append(IP)
            print ipLIST
    print ipLIST

IP_Range()

:

Please enter IP or Network, enter "end" to break: 123.45
['123.45']
Please enter IP or Network, enter "end" to break: 678.90
['123.45', '678.90']
Please enter IP or Network, enter "end" to break: 1.2.3.4
['123.45', '678.90', '1.2.3.4']
Please enter IP or Network, enter "end" to break: end
['123.45', '678.90', '1.2.3.4']
+5

ips, iter comp:

def ip_range():
    return [ip for ip in iter(lambda:
        raw_input('Please enter IP or Network, enter "end" to break: '),"end")]

, "end" .

ip, :

def ip_range():
    ips = [] # create outside the loop
    for ip in iter(lambda:
        raw_input('Please enter IP or Network, enter "end" to break: '), "end"):
        ips.append(ip)
        print(ip)
    print(ip)
    return ips

- , , , , , .

+1

: . while. :

def IP_Range():
    ipLIST = []
    while True:
        IP = raw_input('Please enter IP or Network, enter "end" to break: ')
        if IP == 'end':
            break
        else:
            ipLIST.append(IP)
            IP = ""
            print ipLIST
    print ipLIST

IP_Range()
0

, , , ( , , , "", OP). , , ipLIST while ( , , ), :

def IP_Range():
    while True:
        IP = raw_input('Please enter IP or Network, enter "end" to break: ')
        if IP == 'end':
            break
        else:
            try:
                ipLIST.append(IP)  # Append the IP to the list.
            except NameError:
                ipLIST = [IP,]  # Create the list, if it doesn't already exist.
            print ipLIST
    print ipLIST

IP_Range()

:

Please enter IP or Network, enter "end" to break: 1.2.3.4
['1.2.3.4']
Please enter IP or Network, enter "end" to break: 2.3.4.5
['1.2.3.4', '2.3.4.5']
Please enter IP or Network, enter "end" to break: 3.4.5.6
['1.2.3.4', '2.3.4.5', '3.4.5.6']
Please enter IP or Network, enter "end" to break: end
['1.2.3.4', '2.3.4.5', '3.4.5.6']
>>>

, , while (try ... except), , , (ipLIST = []).

I did this from time to time when I did not need to initialize the empty ones list, dictor something else. This is what you should have in your toolbar, just in case you need it.

0
source

Source: https://habr.com/ru/post/1584896/


All Articles