Reputation:
I created a dictionary and want to create a function called count_type
that will take that dictionary and return another dictionary that is called count
. This function takes in a menu
(dictionary) and returns a count (dictionary) that maps each item type to the count of how many items of that type exist in the menu.
The new dictionary starts looking like:
count = {"Entrees": 0, "Salads": 0, "Sides": 0,
"Kid's Meals": 0, "Desserts": 0, "Drinks": 0}
I want the end result to be the same dictionary but the 0's to be changed to the count of how many times there is a key:value with the value 'Entrees' etc in the original dictionary. an example of what I would like the output to look like:
count = {"Entrees": 3, "Salads": 15, "Sides": 3,
"Kid's Meals": 6, "Desserts": 4, "Drinks": 5}
So far I have the code:
def count_type(carte):
count = {"Entrees": 0, "Salads": 0, "Sides": 0,
"Kid's Meals": 0, "Desserts": 0, "Drinks": 0}
menu = read_file("menu1.csv")
ent = 0
salad = 0
side = 0
kid = 0
dessert = 0
drink = 0
for value in menu:
if value == 'Entrees':
ent += 1
elif value =='Salads':
salad += 1
elif value =='Sides':
side +=1
elif value== "Kid's Meals":
kid +=1
elif value =='Desserts':
dessert +=1
else:
drink +=1
This loop only gives me a count for drink
of 46 which is all the values. How do I do this?
The read_file function that I have created already is:
def read_file(filename):
file = open("menu1.csv", "r", encoding='utf-8-sig')
file.readline()
menu = {}
for line in file:
line = line.rstrip('\n')
line = line.split(',')
item = line[0]
price = float(line[1])
cal = int(line[2])
typ = str(line[3])
lst= (price, cal, typ)
tup = tuple(lst)
menu[item] = tup
return menu
print(menu)
returns a long dictionary:
{'Chic-fil-A Chicken Sandwich': (3.75, 440, 'Entrees'),
'Chic-fil-A Deluxe Sandwich': (4.45, 500, 'Entrees'),
'Spicy Chicken Sandwich': (3.99, 460, 'Entrees'),
'Spicy Deluxe Sandwich': (4.69, 550, 'Entrees'),
'Grilled Chicken Sandwich': (5.15, 320, 'Entrees'),
'Grilled Chicken Club': (6.55, 460, 'Entrees')
Where some are 'Entrees'
, 'Salads'
, etc.
Upvotes: 0
Views: 189
Reputation: 104852
Your code doesn't work because you're iterating over the keys of your menu
dictionary, which is the names of the items (e.g. 'Spicy Chicken Sandwich'
), but you're expecting to have an item type (e.g. "Entrees"
).
To fix this you need to change your loop. I'd suggest:
for _, _, value in menu.values():
if value == 'Entrees':
...
I'd note that the name value
is not very clear what kind of value it is, especially when you're getting a bunch of other data from the dictionary (and throwing them into the _
variable that will be ignored). Unpacking into more clearly named variables, like price, cal, typ
again, might make the code clearer.
It might also be a good idea to match 'Drinks'
explicitly, rather than letting any invalid types fall in there. You could have your code raise an exception if it gets an unknown type instead:
elif value == 'Drinks':
drinks += 1
else:
raise ValueError("Unknown menu item type: {}".format(typ))
Upvotes: 1
Reputation: 2328
In your for loop
for value in menu: instead of checking against value, check against the following
for value in menu:
count[menu[value][2]] += 1
and as mentioned above, instead of creating seperate counters for each variables, you can make use of the already created count dictionary by using
count["Entrees"] += 1
Upvotes: 0
Reputation: 66
I recommend you try: count[value] += 1
in for value in menu
loop
Update: you should edit your loop
for k,v in menu.items():
count[v[2]] += 1
Upvotes: 1