现在,您有一个函数可以返回:('The text contains ',
21,
' alphabetic characters, of which ',
7,
' (',
33.333333333333336,
"%) are 'e'.")
您误解了这个值的一个方面,因为它不是一个字符串,而是一个对象的元组。您希望数据如下所示:
^{pr2}$
这是字符串,不是元组。在
解决办法很简单。在函数中,必须返回字符串而不是元组。你应该这样做:def analyze_text(text):
...
result = "The text contains ", alpha_count, " alphabetic characters, of which ", ecount," (", letter_percent(ecount, alpha_count),"%) are 'e'." # what you're currently returning
return ''.join(map(str, result)) # what you actually want to return
现在,尝试调用函数:In [190]: analyze_text("Blueberries are tasteee!")
Out[190]: "The text contains 21 alphabetic characters, of which 7 (33.333333333333336%) are 'e'."
您还可以使用str.format,或旧样式的字符串格式作为将数据插入字符串占位符的方法。在