Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to add user input in dictionaries?

I can't understand how to add user input in dict:

Lets say:

I want driver to be the key: Vehicle assigned to the driver to be a value:

driver = input("Driver>") vehicle = input("Vehicle>")

I want these to in a dict:

database = {driver : vehicle }

2 Answers

Hi Petko,

It looks like you're not quite thinking about dictionaries in the right way. Dictionaries should be key-value pairs, like this:

{'driver': 'Petko', 'vehicle': 'Porche Cyanne'}

Where 'driver' and 'vehicle' are the keys you use to look up the values 'Petko' and 'Porche Cyanne'. You would look these up with database['driver'], for example.

If you wanted to do that with user input you would do something similar:

driver_given = input("Driver> ")
vehicle_given = input("Vehicle> ")
database = {}
database['driver'] = driver_given
database['vehicle'] = vehicle_given
# {'driver': 'Greg', 'vehicle': 'Honda Civic'}, for example. I'm a man of simple tastes when it comes to cars.

Note this may not be exactly what you want, but the concept of key and value is one should keep in mind.

Hope this is helpful!

Cheers :beers:

-Greg

i would assume you would do something like this:

driver = input('driver >>  ')
vehicle = input('vehicle >>  ')
combo = {}
combo[driver] = vehicle