1

I'm creating a program that calculates the total cost of a meal by taking the following inputs: meal_cost,tax_rate,tip_rate,number_eating

and printing them inside a string with a function call. I looked up on StackOverflow, but could not find a question that suited my situation (printing a dictionary returned from a function in a string)

I have a single function that takes all the inputs and returns a dictionary output. I want to print those returned values in a string with a function call, all in the same line. This is what I tried:

def calculatedCost(meal_cost,tax_rate,tip_rate,number_eating):
  tax = round(float(meal_cost * tax_rate) / 100,2)
  tip = round(float(meal_cost * tip_rate) / 100,2)
  total_cost = round(meal_cost + tax + tip,2)
  division = round(total_cost / number_eating,2)
  return {'tax': tax, 'tip': tip, 'total_cost':total_cost, 'division':division} 

print("The cost of your meal is: {total_cost}, the tax on your meal is: {tax}, the tip is equal to: {tip}, and the split total is: {division}".format(calculatedCost(62.75,5,20,2)))

and I get this error: (I'm using Processing)

KeyError: total_cost
processing.app.SketchException: KeyError: total_cost
at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:240)
at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:119)
at java.lang.Thread.run(Thread.java:748)
CodeMan
  • 23
  • 5

1 Answers1

5

you need to unpack the dict (note the double asterisk):

print("The cost of your meal is: {total_cost}, the tax on your meal is: {tax}, the tip is equal to: {tip}, and the split total is: {division}".format(**calculatedCost(62.75,5,20,2)))
buran
  • 13,682
  • 10
  • 36
  • 61