+ 4

How to make Python continue after if statement

I'm testing if "l" or "r" are in a message and replacing it. It stops after the if statement. If I input: "play pirate", it should return "pway piwate" but it only replaces the word "play". How do I do it for all words? https://code.sololearn.com/cqPGr7nvf9Ir/?ref=app

12th Aug 2020, 9:31 AM
Clueless Coder
Clueless Coder - avatar
8 Antworten
+ 10
You can just replace the *elif* with an *if*. Doing so will make the code to check both of the conditions instead of only one. https://code.sololearn.com/cqXv04YyvTF1/?ref=app
12th Aug 2020, 9:55 AM
Arsenic
Arsenic - avatar
+ 8
Thanks Arsenic and Slick . Didn't know it was so obvious
12th Aug 2020, 10:04 AM
Clueless Coder
Clueless Coder - avatar
+ 7
"". join["w" if c in["l", "r"] else c for c in list(message)] https://code.sololearn.com/cP8mz8YnWilg/?ref=app
12th Aug 2020, 10:40 AM
Oma Falk
Oma Falk - avatar
+ 7
A bit late with my post, but it shows a simplified version of the code. There are 2 conditional statements to check if the desired characters are in the input string. You can just omit them. You can just call the 2 replace() functions. It will be faster and do the job. def uWuFy(msg): msg = msg.replace("l", "w") msg = msg.replace("r", "w") return msg msg = input() print(uWuFy(msg))
12th Aug 2020, 11:49 AM
Lothar
Lothar - avatar
+ 5
i used a for loop to iterate the words, then used your conditionals to change the letters on each. it may also have to do with the "elif" statement. Because you checked both words for the occurence of "l" first. each time, you check BOTH words, so that test always comes back true, meaning you will never reach the "elif" statement https://code.sololearn.com/cTTvoW9PCLJo/?ref=app
12th Aug 2020, 9:52 AM
Slick
Slick - avatar
+ 4
Lol Arsenic i just edited mine.. Im not trying to be a duck i swear.
12th Aug 2020, 9:58 AM
Slick
Slick - avatar
+ 2
Steve Barongo Mong'are That was the cause of the issue before!
13th Aug 2020, 8:33 AM
Clueless Coder
Clueless Coder - avatar
- 2
Use elif
13th Aug 2020, 8:04 AM
Steve Barongo Mong'are
Steve Barongo Mong'are - avatar