0
Could someone debug the code, the output should be: Hello My name is Mark
text = 'HelloMyNameIsMark' # text is a string # using list comprehension to separate each word in text. modified_text = "".join( [letter if letter.islower() else " " + letter.lower() if letter in ['N','I'] else ' ' for letter in text] )[1:] print(modified_text)
27 ответов
+ 4
Okay, fair. I assumed it was Python based on the fact that the OP (Original Poster) has taken a few Python courses. And I've been helping on this platform for years. I recognize a pattern. Although I don't believe we need to see the file extension or file type each time someone shows a line of code. I agree that properly adding tags that reflect the coding language is helpful.
+ 5
Aaron Paul You almost had it right
[letter if letter.islower() else " " + letter if letter in ['N', 'I'] else " " + letter for letter in text]
+ 5
'list comprehension' in the tag would be a hint that it's Python.
+ 4
Yash Thale
it's called list comprehension. It is basically if-else statements written inside a list [ ] so you get a list of elements that conforms to those conditional expressions.
https://www.w3schools.com/JUMP_LINK__&&__Python__&&__JUMP_LINK/python_lists_comprehension.asp
+ 3
RuntimeTerror It is python code
+ 3
😁😁😁, yeah. Might have been Mojo.
+ 2
you really don't need a list if you're going to join it to a string in the end.
alternative form using generator expression instead of list comprehension and a simple string 'NI' instead of list ['N','I'] for the 'in' condtion. I also rearranged the condtions a bit:
modified_text = ''.join((' '+x.lower() if x in 'NI' else ' '+x if x.isupper() else x for x in text))[1:]
+ 2
Bob_Li that is not obvious enough. 🕵️♂️
+ 2
Chris coder your code ran successfully, it'd been giving me headache.
+ 2
Thanks Aaron Paul i'm happy to help.
+ 2
Why is this discussion becomes a comment war?
+ 1
What you posted can't be debugged cos it's not a code. I'll suggest you attach the actual code so it will be accessible
+ 1
RuntimeTerror
No, I'm not taking sides. I'm trying to defuse the animosity by adding a bit of lighthearted banter.
You're correct in requesting for a definitive coding language in the tag. Wrong assumptions are a common pitfall, especially in coding.
Chris Coder and I making the assumption that it's Python is perhaps a bit hasty, but the OP can correct us if we were wrong.
The trouble with posting and written text is that the tone of the message is hard to convey. Misinterpretation of emotion is very common. What is supposed to be a lighthearted remark might be taken as a disparaging statement by the other party. So we end up with... this snowball.