FAQ: Learn Python: Function Arguments - Keyword Argument Unpacking

This community-built FAQ covers the “Keyword Argument Unpacking” exercise from the lesson “Learn Python: Function Arguments”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Keyword Argument Unpacking

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

2 posts were split to a new topic: Where does create_product come from?

A post was merged into an existing topic: Where does create_product come from?

Does anyone know why the integers need to be written as a string in Line 16? Thanks in advance.

you loop over the items in your products_dict (in your create_products function), if you use Baseball, this key/name becomes an undefined variable, that is a problem.

I see. But why will Baseball become an undefined variable? Why this issue can be solved by adding ’ '?

Okay, I am very confused. I tried to run a replicate of your code:

def create_products(**products_dict):
  print(products_dict)
  for name, price in products_dict.items():
    print(name, price)
    create_product(name, price)

# Checkpoint 3
# Update the call to `create_products()` to pass in this dictionary as a series of keyword
create_products(
  Baseball = 3,
  Shirt = 14,
  Guitar = 70,
)

which unpacks the keyword arguments in a dictionary just fine.

Could you copy and paste your code to the forum? So I can actually run the code. Preferable both scripts/files.

# Checkpoint 1
print("My name is {name} and I'm feeling {feeling}.".format(
	name = 'Vella', feeling = 'great'
))

# Checkpoint 2
from products import create_product

# Update create_products() to take arbitrary keyword arguments
def create_products(**products_dict):
  for name, price in products_dict.items():
    create_product(name, price)

# Checkpoint 3
# Update the call to `create_products()` to pass in this dictionary as a series of keyword
create_products(
  'Baseball '= 3,
  'Shirt' = 14,
  "Guitar" = 70,
)

The error msg is: SyntaxError: keyword can’t be an expression

Sorry, I’m not sure how to copy & paste the code. If it’s still not clear, could you please tell me the effective way to copy & paste the code? Thanks!

You either use keyword arguments like I did:

create_products(
  Baseball = 3,
  Shirt = 14,
  Guitar = 70,
)

or you use a dictionary like the exercise. You can’t combine these syntax. They are very different

Hi — I already reported this to bugs — this exercise runs with no errors but I get an error message I can’t understand.

How do I get credit for this exercise and move on?

Never mind. I pressed the view solution button and see what I did wrong. I’m glad that the view solution option doesn’t nuke your code anymore–it just shows the difference between your codes and theirs.

So, the working code is here.
I was confused the same as you guys, but I restarted my exercise and did it again.

# Checkpoint 1
print("My name is {name} and I'm feeling {feeling}.".format(name="Ruslan", feeling="good"
	# add your arguments to .format()
))

# Checkpoint 2
from products import create_product

# Update create_products() to take arbitrary keyword arguments
def create_products(**products_dict):
  for name, price in products_dict.items():
    create_product(name, price)

# Checkpoint 3
# Update the call to `create_products()` to pass in this dictionary as a series of keyword
create_products( Baseball=3,
  Shirt=14,
  Guitar=70,
)