+ 2

Please give me the reason for this error

text = "Amy has 128 dollars, while David has 54 dollars. Amy is going to the zoo. David watches soccer." Split_text=text.split(" ") O_text=input() R_text=input() count=0 for i in range(len(Split_text)): e_list=Split_text[i] if "," in Split_text[i] and (e_list[:-1]==O_text): Split_text[i]=R_text+"," count=count+1 if ("." in Split_text[i]) and (e_list[:-1]==O_text) : Split_text[i]=R_text+"." count=count+1 if(Split_text [i]==O_text): Split_text [i]=R_text count=count+1 print(count) for i in range(len(Split_text)): if(i==len(Split_text)-1): print(Split_text[i]) else: print(Split_text[i],end=" ") In the above program(manipulating the substring with another) 4 test cases are correct but one test is an error.. Due to hide of that case I can't solve it. so please give the solution for it.

28th Apr 2023, 1:53 PM
RAMETI SIVAMANI
11 Réponses
+ 10
RAMETI SIVAMANI , > the code as it is done, does not replace "," or "." by a replacement. > the code is also far to complicated. you can try to use <string>replace(... , ...) to get it done. we also do not need to split the input sentence. > to get the number of replacements we can use <string>.count(...)
28th Apr 2023, 2:52 PM
Lothar
Lothar - avatar
+ 7
RAMETI SIVAMANI , the task / tutorial you are talking about is: > *python for data structures*, > chapter *working with strings* > lesson *manipulating strings*, > exercise number *5.2*
28th Apr 2023, 2:29 PM
Lothar
Lothar - avatar
+ 5
RAMETI SIVAMANI yes, I agree with Lothar. You may be able to solve it your way, but you are not really learning Python. You are just using generic programming. Write Python the Python way and you will be surprised how simple it becomes.
28th Apr 2023, 3:01 PM
Bob_Li
Bob_Li - avatar
+ 4
RAMETI SIVAMANI Pls edit your question description and add: 1. The task description 2. A link to your code in Code Playground (save the code there first) So we know what you're trying to do and can test your code and possible solutions.
28th Apr 2023, 1:58 PM
Emerson Prado
Emerson Prado - avatar
+ 3
RAMETI SIVAMANI Learning should be primary. Solving the challenge is secondary. You can speedrun through the course. But what do you get out of it? Sololearn certificate. Big deal. Nobody cares about it. Finishing the course but not really learning it is a waste of time. I have most of them and I'm still learning the nuances of each language. Each have their strength and weakness. You have to learn how to use them effectively.
28th Apr 2023, 3:41 PM
Bob_Li
Bob_Li - avatar
+ 2
RAMETI SIVAMANI Lothar said it, your code cannot handle punctuation replacement. It also cannot handle single character or substring replacement.
28th Apr 2023, 3:30 PM
Bob_Li
Bob_Li - avatar
+ 1
Yes .. I written that code as in this form but only one test case shows an error
28th Apr 2023, 2:31 PM
RAMETI SIVAMANI
+ 1
I want to write the program without the using string methods[count and replace]
28th Apr 2023, 3:21 PM
RAMETI SIVAMANI
+ 1
RAMETI SIVAMANI But then you are not really learning Python. And the program is not as beautiful as it can be. Also, you are coding more things that can be handled automatically, introducing more points of error. Why do less with more lines of code when you can do more with less lines of code?🥲 My solution can be written inside the print() function.
28th Apr 2023, 3:25 PM
Bob_Li
Bob_Li - avatar
+ 1
Whether I'm learning or not that is secondary.... I need to know that how it is going to solve with logic and without those two methods
28th Apr 2023, 3:28 PM
RAMETI SIVAMANI
0
Okkk
28th Apr 2023, 3:33 PM
RAMETI SIVAMANI