Python函数未按预期返回

时间:2015-04-27 12:24:53

标签: python python-2.7

我正在尝试创建一个简单的函数来运行while循环,并将输入的IP地址附加到列表中以供其他用途。我在print语句中看到的是,我只将最近输入的IP附加到列表变量,列表的最后一个打印返回一个空白列表。

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()

先谢谢你,我知道这很简单,我忽略了一些显而易见的事情。你可以告诉我,我是Python和编程的新手。

4 个答案:

答案 0 :(得分:5)

乍一看,看起来你应该将列表初始化放在循环之外。如果你在循环中执行ipLIST = [],那么它将在每次迭代后重置为空列表。

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']

答案 1 :(得分:1)

如果您只想收集一系列ips,可以使用iter和list 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,你可以使用普通的for循环:

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

打印和返回是两个非常不同的东西,所以如果你想在其他地方使用列表确保你返回它,你也应该使用小写和下划线表示变量名。

答案 2 :(得分:0)

问题是:每次在while循环中初始化新的空列表时循环。 您可以在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()

答案 3 :(得分:0)

The solution proposed by Kevin is probably the best (it got my vote, it's the way I'd usually do it, and it would get my "Accept," if I were the OP). However, if you've got a compelling reason that you absolutely must declare ipLIST inside your while loop (I can't think of one in this case, but that doesn't mean that you don't have one), then you can do it as follows:

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()

Results are:

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']
>>>

This works because, although you're creating the list inside the while loop (the try ... except), you're not reinitializing it every time you go through the loop, as you did in your original code (ipLIST = []).

I've done this from time to time when I've needed to avoid initializing an empty list, dict, or whatever. It's something you should have in your toolbox, just in case the need comes up.