[英]How to make arguments non-specific in python?
我在 python 中有一個披薩店成本計算器的代碼,它計算下訂單后的成本,其中可以包括披薩、飲料、雞翅和優惠券。 不一定要有所有這些 arguments,它會有各種各樣的。 這就是我卡住的地方。 我有代碼,但我需要使用默認值 Arguments 來制作它,以便插入任何數量的 arguments 都會產生有效的 output。 (關於位置參數的東西)
這是代碼:
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.
當您聲明一個不使用默認 arguments 的 function 時,Python 希望您必須為每個參數輸入一個輸入。 默認參數將允許您不為參數添加任何內容。
def cost_calculator(pizza=[], drinks=[], wings=[], coupon=0.0):
''' your code here '''
非默認 arguments 被稱為位置 arguments 因為它們總是必須輸入並且必須以正確的順序輸入。
對於默認 arguments,您應該確保在進行 function 調用時輸入參數的名稱:
cost_calculator(drinks=['small', 'small', 'large'], coupon=0.2)
我認為這應該在分級代碼中起作用。 他們正在使用一種稱為字典解包的方法,該方法允許您將所有默認參數作為字典 object 提交,按慣例稱為kwargs :
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.