Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数中任意数量的用户输入_Python_Args - Fatal编程技术网

Python 函数中任意数量的用户输入

Python 函数中任意数量的用户输入,python,args,Python,Args,我创建了一个Python函数,它接受任意数量的整数输入并返回LCM。我想让用户以友好的方式向我传递任意数量的输入,然后让我的函数计算它们 我找到了一种合理的方法让用户一次传递一个整数并将它们附加到列表中,但是,我似乎无法让我的函数将其作为列表或元组进行处理 这是我的密码: #Ask user for Inputs inputs = [] while True: inp = input("This program returns the LCM, Enter an integer,\

我创建了一个Python函数,它接受任意数量的整数输入并返回LCM。我想让用户以友好的方式向我传递任意数量的输入,然后让我的函数计算它们

我找到了一种合理的方法让用户一次传递一个整数并将它们附加到列表中,但是,我似乎无法让我的函数将其作为列表或元组进行处理

这是我的密码:

#Ask user for Inputs
inputs = []
while True:
    inp = input("This program returns the LCM, Enter an integer,\
    enter nothing after last integer to be evaluated: ")
    if inp == "":
        break
    inputs.append(int(inp))

#Define function that returns LCM
def lcm(*args):
    """ Returns the least common multiple of 'args' """
    #Initialize counter & condition
    counter = 1
    condition = False

    #While loop iterates until LCM condition is satisfied
    while condition == False :
        counter = counter + 1
        xcondition = []
        for x in args:
            xcondition.append(counter % x == 0)
        if False in xcondition:
            condition = False
        else:
            condition = True
    return counter

#Execute function on inputs
result = lcm(inputs)

#Print Result
print(result)

你需要打开你的清单

result = lcm(*inputs)

但总的来说,我想说的是,接受单个序列(
list
tuple
,等等)参数比担心
*arg
解包更具python风格。
args
的思想是获取任意数量的参数,并将其作为列表处理,以便于处理

但您只插入一个参数-列表


使用
lcm(*inputs)
(将列表解压为不同的参数)或仅将列表作为参数(意思是
lcm
仅定义为
lcm(args)
)。

解压列表:
result=lcm(*inputs)
可以解压列表,但如果要使用列表,为什么不设计一个接受列表的函数呢?啊,Splat之类的东西。一个字符的修复-谢谢大家!!