Counting and Grouping function with Python (2.7) -


i have tried following function:

def item_order(order):     salads = order.count("salad")     hamburgers = order.count("hamburger")     waters = order.count("water")     return "salad:{} hamburger:{} water:{}".format(salads, hamburgers, waters) 

taken ( https://stackoverflow.com/questions/34906570/counting-and-grouping-with-python ), these 2 orders:

1st order = "salad water hamburger salad hamburger"
- function should returns "salad:2 hamburger:2 water:1"

2nd order = "hamburger water hamburger"
function should returns "salad:0 hamburger:2 water:1",

in http://www.pythontutor.com/visualize.html#mode=edit

but seems doesn't work. maintaining structure, doing wrong?

many help!!

you have function definition, , in script define order:

def item_order(order):     # function here  order = 'salad water hamburger salad hamburger' 

when call function, need either assign result variable or otherwise display return function. so:

print item_order(order) 

or:

x = item_order(order) print x 

Comments