I’m working on a Python assignment which requires a text to be delimited, sorted and printed as:
- sentences are delimited by
. - phrases by
, - then printed
What I’ve done so far:
text = "what time of the day is it. i'm heading out to the ball park, with the kids, on this nice evening. are you willing to join me, on the walk to the park, tonight."
for i, phrase in enumerate(text.split(',')):
print('phrase #%d: %s' % (i+1,phrase)):
phrase #1: what time of the day is it. i'm heading out to the ball park
phrase #2: with the kids
phrase #3: on this nice evening. are you willing to join me
phrase #4: on the walk to the park
phrase #5: tonight.
I know a nested for loop is needed and have tried with:
for s, sentence in enumerate(text.split('.')):
for p, phrase in enumerate(text.split(',')):
print('sentence #%d:','phrase #%d: %s' %(s+1,p+1,len(sentence),phrase))
TypeError: not all arguments converted during string formatting
A hint and/or a simple example would be welcomed.
There are couple of issues with your code snippet
If I understand you correctly, you want to split the sentence by delimited
.. Then Each of these sentences you want to split on phrases which is again delimited by,. So second line of your should actually split the output of the outer loops enumeration. Something likeThe Print Statement. If you ever See an Error Like
TypeError, you can be sure that you are trying to assign a variable of one type onto another type. Well but there is no assignments? Its an indirect assignment to the print concatenation. What you committed to the print is that, you would be supplying 3 parameters out of which first two would beIntegers(%d)and the last astring(%d). But you ended up supplying 3Integers(s+1,p+1,len(sentence),phrase) which is inconsistent with your print format specifier. Either you drop the third parameter (len(sentence)) likeor add one more Format Specifier to the print statement
Assuming you want the former want, that leaver us to