如何在 python 中使参数不特定?
How to make arguments non-specific in python?
我在 python 中有一个用于比萨店成本计算器的代码,它计算下单后的成本,其中可以包括比萨饼、饮料、鸡翅和优惠券。它不一定要有所有这些参数,它会有多种多样。这就是我被困的地方。
我有代码,但我需要使用默认值参数来创建它,以便插入的任何数量的参数都将产生有效的输出。 (关于位置参数的一些东西)
这是代码:
def pizza_cost(pizorders):
total = 0
for order in pizorders:
total += 13
if "pepperoni" in order:
total = total + (x.count("pepperoni") * 1)
if "mushroom" in order:
total = total + (x.count("mushroom") * 0.5)
if "olive" in order:
total = total + (x.count("olive") * 0.5)
if "anchovy" in order:
total = total + (x.count("anchovy") * 2)
if "ham" in order:
total = total + (x.count("ham") * 1.5)
return total
def drink_cost(driorders):
total = 0
for order in driorders:
if order == "small":
total = total + (x.count("small") * 2)
if order == "medium":
total = total + (x.count("medium") * 3)
if order == "large":
total = total + (x.count("large") * 3.5)
if order == "tub":
total = total + (x.count("tub") * 3.75)
return total
def wing_cost(wingorders):
total = 0
for order in wingorders:
if order == 10:
total += 5
if order == 20:
total += 9
if order == 40:
total += 17.5
if order == 100:
total += 48
return total
def cost_calculator(*pizzas, *drinks, *wings, *coupon):
total = 0
total += pizza_cost(pizzas)
total += drink_cost(drinks)
total += wing_cost(wings)
tax = total * 0.0625
discount = total * coupon
total += tax
total -= discount
return total
这是错误:
TypeError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/__init__.py in compare_functions(student, soln, fn_args, fn_kwargs, function_name, comparison_function)
110 try:
--> 111 student_out = student(*fn_args, **fn_kwargs)
112 except Exception as e:
TypeError: cost_calculator() missing 3 required positional arguments: 'drinks', 'wings', and 'coupon'
During handling of the above exception, another exception occurred:
StudentError Traceback (most recent call last)
<ipython-input-13-322b790cbb8b> in <module>
1 # Execute this cell to grade your work
2 from bwsi_grader.python.pizza_shop import grader
----> 3 grader(cost_calculator)
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/python/pizza_shop.py in grader(student_func)
191 for pizzas, items in zip(std_pizzas, std_orders):
192 compare_functions(student=student_func, soln=soln,
--> 193 fn_args=tuple(pizzas), fn_kwargs=items)
194
195 for i in range(1000):
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/__init__.py in compare_functions(student, soln, fn_args, fn_kwargs, function_name, comparison_function)
111 student_out = student(*fn_args, **fn_kwargs)
112 except Exception as e:
--> 113 raise StudentError(f"\nCalling \n\t{pad_indent(sig, ln=4)}\nproduces the following error:"
114 f"\n\t{type(e).__name__}:{e}"
115 f"\nPlease run your function, as displayed above, in your Jupyter "
StudentError:
Calling
student_function([])
produces the following error:
TypeError:cost_calculator() missing 3 required positional arguments: 'drinks', 'wings', and 'coupon'
Please run your function, as displayed above, in your Jupyter notebook to get a detailed stacktrace of the error, and debug your function.
当您声明一个不使用默认参数的函数时,Python 希望您必须 每个参数都有一个输入。默认参数将允许您不为参数添加任何内容。
def cost_calculator(pizza=[], drinks=[], wings=[], coupon=0.0):
''' your code here '''
非默认参数称为位置参数,因为它们总是必须输入并且必须以正确的顺序输入。
对于默认参数,您应该确保在进行函数调用时输入参数名称:
cost_calculator(drinks=['small', 'small', 'large'], coupon=0.2)
我认为这应该适用于评分代码。他们正在使用一种称为 dictionary unpacking 的方法,该方法允许您将所有默认参数作为字典对象提交,按照惯例称为 kwargs :
我在 python 中有一个用于比萨店成本计算器的代码,它计算下单后的成本,其中可以包括比萨饼、饮料、鸡翅和优惠券。它不一定要有所有这些参数,它会有多种多样。这就是我被困的地方。 我有代码,但我需要使用默认值参数来创建它,以便插入的任何数量的参数都将产生有效的输出。 (关于位置参数的一些东西)
这是代码:
def pizza_cost(pizorders):
total = 0
for order in pizorders:
total += 13
if "pepperoni" in order:
total = total + (x.count("pepperoni") * 1)
if "mushroom" in order:
total = total + (x.count("mushroom") * 0.5)
if "olive" in order:
total = total + (x.count("olive") * 0.5)
if "anchovy" in order:
total = total + (x.count("anchovy") * 2)
if "ham" in order:
total = total + (x.count("ham") * 1.5)
return total
def drink_cost(driorders):
total = 0
for order in driorders:
if order == "small":
total = total + (x.count("small") * 2)
if order == "medium":
total = total + (x.count("medium") * 3)
if order == "large":
total = total + (x.count("large") * 3.5)
if order == "tub":
total = total + (x.count("tub") * 3.75)
return total
def wing_cost(wingorders):
total = 0
for order in wingorders:
if order == 10:
total += 5
if order == 20:
total += 9
if order == 40:
total += 17.5
if order == 100:
total += 48
return total
def cost_calculator(*pizzas, *drinks, *wings, *coupon):
total = 0
total += pizza_cost(pizzas)
total += drink_cost(drinks)
total += wing_cost(wings)
tax = total * 0.0625
discount = total * coupon
total += tax
total -= discount
return total
这是错误:
TypeError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/__init__.py in compare_functions(student, soln, fn_args, fn_kwargs, function_name, comparison_function)
110 try:
--> 111 student_out = student(*fn_args, **fn_kwargs)
112 except Exception as e:
TypeError: cost_calculator() missing 3 required positional arguments: 'drinks', 'wings', and 'coupon'
During handling of the above exception, another exception occurred:
StudentError Traceback (most recent call last)
<ipython-input-13-322b790cbb8b> in <module>
1 # Execute this cell to grade your work
2 from bwsi_grader.python.pizza_shop import grader
----> 3 grader(cost_calculator)
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/python/pizza_shop.py in grader(student_func)
191 for pizzas, items in zip(std_pizzas, std_orders):
192 compare_functions(student=student_func, soln=soln,
--> 193 fn_args=tuple(pizzas), fn_kwargs=items)
194
195 for i in range(1000):
~/opt/anaconda3/lib/python3.7/site-packages/bwsi_grader/__init__.py in compare_functions(student, soln, fn_args, fn_kwargs, function_name, comparison_function)
111 student_out = student(*fn_args, **fn_kwargs)
112 except Exception as e:
--> 113 raise StudentError(f"\nCalling \n\t{pad_indent(sig, ln=4)}\nproduces the following error:"
114 f"\n\t{type(e).__name__}:{e}"
115 f"\nPlease run your function, as displayed above, in your Jupyter "
StudentError:
Calling
student_function([])
produces the following error:
TypeError:cost_calculator() missing 3 required positional arguments: 'drinks', 'wings', and 'coupon'
Please run your function, as displayed above, in your Jupyter notebook to get a detailed stacktrace of the error, and debug your function.
当您声明一个不使用默认参数的函数时,Python 希望您必须 每个参数都有一个输入。默认参数将允许您不为参数添加任何内容。
def cost_calculator(pizza=[], drinks=[], wings=[], coupon=0.0):
''' your code here '''
非默认参数称为位置参数,因为它们总是必须输入并且必须以正确的顺序输入。
对于默认参数,您应该确保在进行函数调用时输入参数名称:
cost_calculator(drinks=['small', 'small', 'large'], coupon=0.2)
我认为这应该适用于评分代码。他们正在使用一种称为 dictionary unpacking 的方法,该方法允许您将所有默认参数作为字典对象提交,按照惯例称为 kwargs :