我呼吁item_order(顺序)计算,以字符数,然后一个函数int应该回报是这样的:返回字符串,并从功能
'Letter a: number of letter a letter b: number of letter b'
但我得到一个错误,说我不能连接str和int。 如何返回str和int?
这是代码(我使用python):
def item_order(order):
'''
order is a string containing words for the items a customer can order
returns the number of times each word is listed
with this format : name : number of times it is listed
'''
s=0
h=0
w=0
for char in order:
if char=='s':
s=s+1
if char=='h':
h=h+1
if char=='w':
w=w+1
answer='salad:', s 'hamburger:' h 'water:' w
return answer
当我调用该函数与这种说法('salad, salad, hamburger, water')
我希望它返回此:
'salad:2 hamburger:1 water:1'
我能找到正确的每个单词列出的次数,但不能用上述格式返回。
从你所提到的要求
请提供代码片段。 –
你使用什么编程语言? –
请提供说明您的问题的[最低,完整示例](http://stackoverflow.com/help/mcve)。 – Sam