0

I try to use but no output:

d = {'a': 1,'b': 2}
value = input()
result = None;
for key in d:
    if d.get(key) == value:
          print(key)

What's the problem?

And also, what's the difference between d['1'] and d.get('a')?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
  • correction: d['a'] – Aleksey Jan 22 '22 at 12:28
  • Don't post corrections to your question in the comment section. You can [edit](https://stackoverflow.com/posts/70812809/edit) your own question. – Ted Klein Bergman Jan 22 '22 at 12:31
  • For starters, `input()` returns a `str`. (Intentionally left vague to encourage further research). And `1 == '1'` will always be `False`. – S3DEV Jan 22 '22 at 12:32
  • You dont find keys by values - thats not what dicts are for. The difference between `d['a']` and `d.get('a')` is documeted in the get method and important if the key you access is NOT in the dict. – Patrick Artner Jan 22 '22 at 12:33
  • Closed with 3 different duplicates. Please read all 3. – Bharel Jan 22 '22 at 12:37

1 Answers1

0

For you first question, the issue is that you don't convert your input to an integer:

value = int(input())

Remember, in Python '1' != 1.

For your second question, see this duplicate question.

Bharel
  • 23,672
  • 5
  • 40
  • 80