+ 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.
5 odpowiedzi
+ 3
That's true, but it'll only ever print that if the condition is true
+ 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")
+ 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.
0
that'll work but its not accessing the key from the dictionary. its just printing the value you entered.
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