Reputation: 319
I have got this code:
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
print(mylist[char])
and it works fine, but when I change it to this:
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for char in mylist:
if mylist[char] == char:
print(mylist[char])
it dos not return any value, neither gives error message.
What is missing or wrong?
Thank you!
BR, Valters
Upvotes: 0
Views: 739
Reputation: 74
yes it wont work the reason is
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ') // <- here you are assigning the user input to the variable char.
for char in mylist: // <- the variable char is again assigned different value i.e the keys from the dictionary
if mylist[char] == char: // <- so here in first iteration the char will be "a" in " mylist[char] ", which is compared with char "a" , so the condition is as follows.
mylist["a"] == a // <- but here the value will be " 1 == a " since this condition fails
print(mylist[char]) // <- the print statement is not executed.
Upvotes: 0
Reputation: 1939
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for char in mylist:
if mylist[char] == char:
print(mylist[char])
The problem with this is, when you are looping through a dictionary, you are checking if key==value
which returns false and so nothing gets printed.
for char in mylist
Here the char value is the keys so for your approach you have to modify the if
condition. Instead of checking keys with value check keys with the entered chars.
So this will be like:
for key in mylist:
if key == char:
print(mylist[char])
And also instead of using loop and if condition, you can simply do as below:
if char in mylist: # it checks if char is present in dict's key or not
print(mylist[char])
Upvotes: 0
Reputation: 4472
The char in the loop scoop is referring to a key of the mylist so when you are doing in the loop if mylist[char] == char
it's checking if the key is equal to the value.
This is how it looks like inside the loop on each iteration.
mylist[char] 1 char a
mylist[char] 2 char b
mylist[char] 3 char c
so in the second example, it's not getting into the if statement that will create you a list of all lines that you can iterate over it over and over again.
You can do
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist:
print(mylist[char])
Upvotes: 0
Reputation: 364
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist.keys():
print mylist.get(char, None)
In your code you are actually trying to compare your key with dictionary value, and there it fails.
I hope I answered your problem.
Upvotes: 1
Reputation: 54148
First, don't call 2 things the same here char
. As you iterate the dict
you get the keys
and it's with these keys that you need to check for equality
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for key in mylist:
if key == char:
print(mylist[key])
But doing that make the all purpose of using a dict
disappear, you loose the performance of just doing mylist[char]
Upvotes: 3