Lowest price ever! Learn Generative AI for 48% less!
Get my discount+ 1
can you debug this code
direct_sentence = " I love programming, said Jane.It us so much fun." direct_to_indirect(direct_sentence) print(indirect_sentence) def direct_to_indirect(direct_sentence): """ Convert direct speech to indirect speech """ words = direct_sentence.split() indirect_sentence = [] for i in range(len(words)): word = words[i] if word in ["said", "told", "asked"]: indirect_sentence.append(word) indirect_sentence.append("that") elif i == 0: indirect_sentence.append(word.capitalize()) elif word.endswith(".") or word.endswith("?") or word.endswith("!"): indirect_sentence.append(word[:-1]) indirect_sentence.append(word[-1]) else: indirect_sentence.append(worhd) return " ".join(indirect_sentence)
4 Answers
+ 3
...here's your code, which now runs:
direct_sentence = " I love programming, said Jane.It us so much fun."
def direct_to_indirect(direct_sentence):
words = direct_sentence.split()
indirect_sentence = []
for i in range(len(words)):
word = words[i]
if word in ["said", "told", "asked"]:
indirect_sentence.append(word)
indirect_sentence.append("that")
elif i == 0:
indirect_sentence.append(word.capitalize())
elif word.endswith(".") or word.endswith("?") or word.endswith("!"):
indirect_sentence.append(word[:-1])
indirect_sentence.append(word[-1])
else:
indirect_sentence.append(word)
return " ".join(indirect_sentence)
print(direct_to_indirect(direct_sentence))
+ 2
Damsara,
Where shall we start? Is this your full code?
1) LN 2 - Function call before definition.
2) LN 32 - Type 'worhd' not 'word'.
3) LN 3 - Printing 'indirect_sentence' before it's definition.
0
Always give information for those willing to help you. At least, what the code should do, and what it's doing wrong.
Also, pls use Code Playground to show code. There, we can run your code, debug and test solutons.
Finally, pls don't overuse line breaks. They make the code hard to read in small screens.
0
Nisha, no need to ask if you can ask … just ask away … ✌️