function - how to omit parenthesis in return from a python def -
hello trying build function count words, return has "salad:0 hamburger:2 water:1" notice each item formatted name of item,a colon symbol,count of item , item groups separated space, getting following ('salad:', 1, 'hamburger:', 2, 'water:', 2) how can take () , '' off of return
my code following:
def item_order (order = ""): order = order s = order.count('salad') h = order.count('hamburger') w = order.count('water') return 'salad:',s, 'hamburger:',h, 'water:',w print item_order('water hamburger water hamburger salad')
as said in previous comments, returning tuple
, which, when printed, produces output getting
('salad:', 1, 'hamburger:', 2, 'water:', 2)
in case want return single string mentioned format can do
return ' '.join(('salad:', str(s), 'hamburger:', str(h), 'water:', str(w)))
which, example, produces
'salad: 1 hamburger: 2 water: 2'
Comments
Post a Comment