19/19


#1

d = { 'a':'key 1', 'b': 'key 2', 'c':'key 3'}

for index, item in enumerate(d):
if d[key] == 'key 3':
print 'you found the key'
break

print index + 1, item

else:
print 'You did not find your key'

the goal of my code is such that when key 3 is found in the dict, a certain line is print.
this is my code, but key is not defined?


#2

Hi!
You should replace d[key] with d['c'].
It should work!


#3

Hi @phybiochem ,

When a dictionary is enumerated, each iteration yields an index, key pair. Each index is an int, and the sequence begins with 0. To access items in the dictionary, you need to use the second item in the index, key pair, which is the key. See the following ...

dct = { 'a':'key 1', 'b': 'key 2', 'c':'key 3', 'd':'key 4', 'e':'key 5'}

for ind, key in enumerate(dct):
    print ind, key
    if dct[key] == 'key 4':
        print 'you found the key'
        break
    else:
        print 'You did not find your key'

Output:

a
You did not find your key
1 c
You did not find your key
2 b
You did not find your key
3 e
You did not find your key
4 d
you found the key

#4

@appylpye
thank you!
but why is it
a c b e d?


#5

even_numbers = [ 6 , 18 , 64 , 88 ]
odd_numbers = [ 3 , 11 , 86 , 99 ]
finaly_numbers = []
for a,b in zip(even_numbers,odd_numbers ) :
c = max(a,b) ** 2
finaly_numbers.append(c)
print(finaly_numbers)
for nr in finaly_numbers :
if nr % 2 == 0 :
print ("%5d This numbers is even" %nr)
else:
print ("%5d This numbers is odd" %nr)

It's my version...i think that this progroam is very simple and is very easy to understand it))

Output

[36, 324, 7396, 9801]
36 This numbers is even
324 This numbers is even
7396 This numbers is even
9801 This numbers is odd