Python Forum
Get a value from a dictionary through input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get a value from a dictionary through input (/thread-23994.html)



Get a value from a dictionary through input - Anony - Jan-26-2020

Hi everyone, I'm new here! I have been programming with python for a month and I'm a beginner. Having to make a project for school, I need to know how I can get, from a previously created dictionary, a value associated with a key by entering the key with an input.
I tried using get(), but I don't know how to combine it with input.

Could you help me please? Thank you in advance


RE: Get a value from a dictionary through input - buran - Jan-26-2020

assign the input to a name (variable)
use that name as argument in the dict.get() method


RE: Get a value from a dictionary through input - Anony - Jan-26-2020

(Jan-26-2020, 05:32 PM)buran Wrote: assign the input to a name (variable)
use that name as argument in the dict.get() method
Thanks for reply. In this way?
dictionary = {"brand": "Ford","model": "Mustang","year": 1964}
x=input("")
dictionary.get(x)
If I run it nothing happen
[Image: NM7nWYQ]

Damn, I’m an idiot. I forgot print ahahahaha. Now it runs, thanks @buran


RE: Get a value from a dictionary through input - buran - Jan-26-2020

you are not doing anythinh with the value returned from the get() method (i.e. it's just returned and then lost)
either assign it to a name, so that you can use it later, or print it, e.g.
value = dictionary.get(x)
# do something, possibly with value
print(value)
# or simply
print(dictionary.get(x))