+ 2

How do I get the quoted part in a dictionary to print out?

chem = {"Cu":12, "Fe":52,"Pb":14} talkback = input("enter a chemical: ") if talkback in chem: print(chem[talkback]) else: print(" try again buddy") I want it to print the quoted part plus the value assigned to it.

1st Oct 2016, 2:10 PM
Avery
Avery - avatar
5 Respostas
+ 3
That's true, but it'll only ever print that if the condition is true
1st Oct 2016, 2:55 PM
Avery
Avery - avatar
+ 2
I figured it out. chem = {"Cu":12, "Fe":52,"Pb":14} talkback = input("enter a chemical: ") if talkback in chem: print(talkback, ":", chem[talkback]) else: print(" try again buddy")
1st Oct 2016, 2:47 PM
Avery
Avery - avatar
+ 1
not the cleanest way but: chem = {"Cu":12, "Fe":52,"Pb":14} talkback = input("enter a chemical: ") if talkback in chem: print(chem[talkback]) print(list(chem)[list(chem).index(talkback)]) else: print(" try again buddy") have to use a list to get the key since we're not iterating over the dictionary. we can always do a list just to extract the keys from the dictionary. you could also iterate through using a loop.
1st Oct 2016, 2:44 PM
alex
alex - avatar
0
that'll work but its not accessing the key from the dictionary. its just printing the value you entered.
1st Oct 2016, 2:54 PM
alex
alex - avatar
0
too true 😜 but it's still not printing the quoted part out of the dictionary. it's coming from the local variable 😊 i guess it depends if the input is immutable or not
1st Oct 2016, 3:04 PM
alex
alex - avatar