Python: turning a tuple from return statement into a string
python, return, tuples
Solution
It's because you're using commas in your return statement, which Python is interpreting as a tuple. Try using `format()` instead:
def wordCount(paragraph):
splited = paragraph.split()
wordnum = len(splited)
eWord = []
for aWord in splited:
if "e" in aWord:
eWord.append(aWord)
eWordnum = len(eWord)
percent = round(eWordnum / wordnum * 100,2)
return "Your text contains {0} words, of which {1} ({2}%) contains an 'e'".format(wordnum, eWordnum, percent)
>>> wordCount("doodle bugs")
"Your text contains 2 words, of which 1 (0.0%) contains an 'e'"
Problem
I've been working on HTTLCS and am having some difficulty finishing up the problem. Solving a problem was not much of an issue, but I have trouble returning my result as a string rather than the tuple data type. Here is my code: ``` def wordCount(paragraph): splited = paragraph.split() wordnum = len(splited) eWord = [] for aWord in splited: if "e" in aWord: eWord.append(aWord) eWordnum = len(eWord) percent = round(eWordnum / wordnum * 100,2) return "Your text contains", wordnum, "words, of which" , eWordnum , "(" , percent , "%)" , "contains an 'e'." print(wordCount(p)) ``` Python outputs `('Your text contains', 108, 'words, of which', 50, '(', 46.3, '%)', "contains an 'e'.")` which is a tuple, not a string. I know I can just put print at the end of the function and call the function without print() statement, but how do I solve this with a return statement?