Reputation: 73
Enclosed is a copy of a question I am trying to answer. (Its not homework btw, just from a programming ebook).
So, in the first instance, I created the dictionary.
fridge ={ "steak" : "it is so yum!" , \
"Pizza" : "it is even yummier!" , \
"eggs": "always handy in a pinch" , \
"ice cream": "a tasty treat for when I work hard" , \
"butter" : "always useful for spreading on toast" \
}
Have to admit, maybe it has been the way the text has been worded, by the sentence:
"Then create a name that refers to a string containing the name of a food, call the name food_sought
badly confused me.
I thought this meant:
create a variable called food_sought, make it equal to any of the keys within the fridge dictionary.... then use a for loop to see if there is a match within the dictionary.
so....
food_sought = "steak"
for food_sought in fridge:
if food_sought !=steak:
print ("there has not been a match!")
Whenever I run the code however, I am told:
Traceback (most recent call last): File "", line 2, in if food_sought ==steak: NameError: name 'steak' is not defined
Upvotes: 0
Views: 426
Reputation: 3895
steak
in this case would be a variable.
What it looks like you're asking for is:
food_sought != 'steak'
but what you probably want is
key != food_sought
See below
If you want the value you can use items()
in python3 or iteritems()
in python 2
food_sought = 'steak'
for key, value in fridge.items():
if key != food_sought:
print("Not the key we're looking for...")
print(key) # the key, ie "steak'
print(value) # the value, ie "it is so yum!" -- I agree
Beef it's what's for dinner.
Upvotes: 2
Reputation: 425
The problem is that steak is a variable, which you haven´t defined.
When you write food_sought!=steak
you are comparing the value of two variables, but variable steak is not defined.
Your first line of code is wrongly assigning food_sought="steak"
, it should be assigning steak='steak'
. That way your code would work:
steak = "steak"
for food_sought in fridge:
if food_sought != steak:
print ("there has not been a match!")
Having said this, how you have written the code is not the best/nicest way of doing, although it works. There is no need of defining the variable steak
, you can compare the food_sought
variable directly against the string 'steak'
.
The code would look like this:
for food_sought in fridge:
if food_sought != 'steak':
print ("there has not been a match!")
Upvotes: 0
Reputation: 22473
Here is how you can iter over a dictionary using a for loop like you want hope it helps you better understand :)
fridge ={ "steak" : "it is so yum!" , \
"Pizza" : "it is even yummier!" , \
"eggs": "always handy in a pinch" , \
"ice cream": "a tasty treat for when I work hard" , \
"butter" : "always useful for spreading on toast" \
}
food_sought = "steak"
for key, value in fridge.items():
if(key == food_sought):
print(key, 'corresponds to', value)
else:
print ("There has not been a match!")
Output: (note dictionaries are not ordered)
There has not been a match!
There has not been a match!
There has not been a match!
There has not been a match!
steak corresponds to it is so yum!
Try it here
Upvotes: 1