如何在不定义任何限制的情况下在python中获取任意数量的输入?

Arp*_*wal 4 python arguments list command-line-arguments python-2.7

现在,问题是,我应该从用户那里获得未知数量的输入,就像在一次运行中他可以输入10个条件在另一次运行中他可以输入40个条件一样。而且我不能最初要求用户输入n的值,因此我可以运行范围循环并开始将输入存储在列表中。如果我能做到这一点,那么我已经创建了循环,但事实并非如此。那么,问题是如何为用户定义端点?或如何将未知数量的参数传递给函数?

def fibi(n):
    while n<0 or n>=50:
        print "Enter value of n greater than 0 but less than 50"
        n = int(raw_input())
    if n==0:
        return n
    else:
        a, b = 0, 1
        for i in range(n):
            a, b = b, a + b
    return a
Run Code Online (Sandbox Code Playgroud)

主调用功能启动

n =[]
????
//This loop is for calling fibi function and printing its output on each diff line
for i in n:
    print (fibi(n[i]))
Run Code Online (Sandbox Code Playgroud)

输入示例:每个条目都应在新行上

1
2
3
4
5
.
.
.
n
Run Code Online (Sandbox Code Playgroud)

样本输出

1
1
2
3
5
Run Code Online (Sandbox Code Playgroud)

Han*_*ila 5

这是从用户读取许多整数输入的方法:

inputs = []
while True:
    inp = raw_input()
    if inp == "":
        break
    inputs.append(int(inp))
Run Code Online (Sandbox Code Playgroud)

如果要将未知数量的参数传递给函数,可以使用* args:

def function(*args):
    print args
function(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)

这将打印(1, 2, 3)

或者,您可以仅使用list来实现此目的:

def function(numbers):
    ...
function([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)