Python Forum
Get a value from a dictionary through input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get a value from a dictionary through input
#1
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
Reply
#2
assign the input to a name (variable)
use that name as argument in the dict.get() method
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(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
Reply
#4
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))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do you replace a dictionary key with a new input? thewetmosquito 4 3,299 Aug-23-2020, 03:48 AM
Last Post: perfringo
  User input & Dictionary tfernandes 5 3,632 Apr-03-2020, 07:12 PM
Last Post: tfernandes
  Problem using input in Dictionary.. Help roseojha 1 1,775 Aug-25-2019, 08:51 AM
Last Post: ThomasL
  QC input value against dictionary in while loop dadgums 2 2,916 Apr-23-2018, 11:38 PM
Last Post: dadgums
  User Input to Choose from Dictionary anelliaf 9 25,753 Mar-27-2018, 02:22 PM
Last Post: anelliaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020