Python 如何接受用户输入的多个参数并将其传递给参数并打印所有项目
一个函数,它接受用户购物列表的多个参数,并打印用户从市场购买的所有项目,但我将列表作为参数传递给它,它显示的不是单个项目的列表:Python 如何接受用户输入的多个参数并将其传递给参数并打印所有项目,python,python-3.x,function,Python,Python 3.x,Function,一个函数,它接受用户购物列表的多个参数,并打印用户从市场购买的所有项目,但我将列表作为参数传递给它,它显示的不是单个项目的列表: (使用任意参数概念给出解决方案) 这里有一些代码: def customer_shopping(*shopping_list): print("\n--------- Shopping List --------") for element in shopping_list: print("You bought: ",elem
def customer_shopping(*shopping_list):
print("\n--------- Shopping List --------")
for element in shopping_list:
print("You bought: ",element)
shopping_list = []
while True:
items = input("\nEnter the item you bought from market \nIf you leave enter'quit': ")
if items == 'quit':
break
shopping_list.append(items)
customer_shopping(shopping_list)
从函数定义中删除解包运算符
*
,然后调用:
def customer_shopping(shopping_list):
...
customer_shopping(shopping_list)
这将产生以下输出:
Enter the item you bought from market
If you leave enter'quit': banana
Enter the item you bought from market
If you leave enter'quit': carrot
Enter the item you bought from market
If you leave enter'quit': quit
--------- Shopping List --------
You bought: Banana
You bought: Carrot
问题是:
def customer_shopping(*shopping_list):
如果您只需删除*它将完全按照您的需要打印 删除函数定义中的
*
就可以了。PLZZZZ使用任意参数概念为我提供解决方案。。。。