-2

I have a code in python for a pizza shop cost-calculator, which calculates the cost after given an order, which can include pizza, drinks, wings, and coupons. It doesn't necessarily have to have all these arguments, it will have a variety. This is where I'm stuck. I have the code, but I need to use Default-Valued Arguments to make it so that any amount of arguments inserted will produce a valid output. (something about positional arguments)

This is the code:

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

And this is the error:

   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.
  • 2
    Post text, not images of text. – Joseph Sible-Reinstate Monica Apr 28 '20 at 03:08
  • 1
    [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](https://meta.stackoverflow.com/a/326590/7509065) – Joseph Sible-Reinstate Monica Apr 28 '20 at 03:10
  • If you want to learn about an unspecified number of positional arguments, take a look at https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters – Eric Truett Apr 28 '20 at 03:11
  • @JosephSible-ReinstateMonica I apologize about the urgent comment, I honestly didn't think I would get a reply at all since this is such a large platform. And @ Eric Truett thank you for the resource, I added "*" in front of my arguments, but I don't that it worked. – ah123412312 Apr 28 '20 at 21:37

1 Answers1

0

When you declare a function that does not use default arguments, Python expects you to have necessarily have an input for every parameter. The default argument will allow you to no put something in for an argument.

def cost_calculator(pizza=[], drinks=[], wings=[], coupon=0.0):

     ''' your code here '''

Non-default arguments are called positional arguments because they always have to be entered and must be entered in the right order.

For default arguments, you should make sure that you are entering the name of the argument when making the function call:

cost_calculator(drinks=['small', 'small', 'large'], coupon=0.2)

I think this should work in the grading code. They are using a method called dictionary unpacking that allows you to hand in all your defaulted parameters as a dictionary object that is referred to as kwargs by convention:

  • I'm not sure that works, because the graders will hand in many random parameters, with or without certain arguments like pizza, drinks, etc. I understand what you're saying but nothing in my code can put something in, it must only be prepared to get a variety of orders. I really appreciate your help and hope you can assist me furthur! – ah123412312 Apr 29 '20 at 04:24
  • @ah123412312 did you try it yet? The case I showed you will make it so you can hand in any random parameters. Please try it and if there is an error show me. – ericstevens26101 Apr 29 '20 at 08:28
  • Yes, I did try it and as I said, I'm pretty sure I can't feed arguments into the code myself, that is what the grader will do. This is the error that showed up: StudentError: Calling student_function([]) produces the following error: TypeError:cost_calculator() takes 0 positional arguments but 1 was given Please run your function, as displayed above, in your Jupyter notebook to get a detailed stacktrace of the error, and debug your function. – ah123412312 Apr 29 '20 at 16:21
  • That code does not feed arguments into your code. It prepares your function to take arguments with a specific name. `positional argument` means one that doesn't have the = to. The ones with equal to are called `keyword arguments`. I guess your function expects 1 positional argument, probably the piazza. I would try: `def cost_calculator(pizza, drinks=[], wings=[], coupon=0.0):` – ericstevens26101 Apr 29 '20 at 18:51
  • Ok, I did try it and it seems to be working, but it does not work when the function is passed only []. The error says that the expected for that is 13.81 dollars, but the function gives out zero. Something similar happens when only a drink and a coupon were given, the function was to give out 3.61 dollars or something, but gave out 16.61 dollars. – ah123412312 Apr 29 '20 at 19:16
  • I found this https://www.reddit.com/r/learnprogramming/comments/bzyxnn/python_help_pizza_shop_assignment/ – ericstevens26101 Apr 29 '20 at 19:23
  • I think all you may need to do is change it to `def cost_calculator(*pizza, drinks=[], wings=[], coupon=0.0):` The star will make all the pizzas go into a list. – ericstevens26101 Apr 29 '20 at 19:39
  • oh it worked! Thank you so much! And the reddit link you sent me is of someone else posting the exact assignment I have (lol). I really appreciate all your help! I have a better understanding of arguments now! Thanks again and stay safe stranger :) – ah123412312 Apr 29 '20 at 22:27