Python 使用函数一次附加多个列表

Python 使用函数一次附加多个列表,python,python-3.x,Python,Python 3.x,我尝试使用列表作为函数中的参数,该函数将用户输入附加到列表中 itemno, itemdescrip, itempr = [], [], [] def inpt(x): n=0 while n < 10: n+=1 x.append(int(input("What is the item number?"))) inpt(*itemno) print(itemno) 当我在函数中输入1时,我期望输出为1,但得到错误:TypeErro

我尝试使用列表作为函数中的参数,该函数将用户输入附加到列表中

itemno, itemdescrip, itempr = [], [], []

def inpt(x):

    n=0
    while n < 10:
        n+=1
        x.append(int(input("What is the item number?")))


inpt(*itemno)
print(itemno)
当我在函数中输入1时,我期望输出为1,但得到错误:TypeError:inpt缺少1个必需的位置参数:“x”

%cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:itemno, itemdescrip, itempr = [], [], []
:
:def inpt(x):
:
:    n=0
:    while n < 10:
:        n+=1
:        x.append(int(input("What is the item number?")))
:
:
:inpt(itemno)
:print(itemno)
:--
What is the item number? 1
What is the item number? 2
What is the item number? 3
What is the item number? 4
What is the item number? 5
What is the item number? 6
What is the item number? 7
What is the item number? 8
What is the item number? 9
What is the item number? 10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

您只需从函数调用中删除*即可

当您在函数调用中用*作为序列前缀时,您会告诉序列;也就是说,将序列的每个成员作为单个参数呈现给函数。在代码中:

inpt(*itemno)
因为itemno是空的,所以您告诉它不要将任何内容解压缩到函数参数中。因此,该函数调用相当于:

inpt()
因为您的inpt函数需要一个参数,所以它会抛出该错误。我不知道您为什么认为需要*这个参数,但简单的修复方法是删除它,这会将列表本身传递给函数:

inpt(itemno)

请随意投票/勾选答案,因为它对我帮助很大;对不起,我忘了: