+ 1

Sololearn code challange Jungle camping (python) problem

I'm practicing coding by solving the code challenge and here is this simple one named jungle camping but I can't get it right The challenge: You are camping alone out in the jungle and you hear some animals in the dark nearby. Based on the noise they make, determine which animals they are. Task: You are given the noises made by different animals that you can hear in the dark, evaluate each noise to determine which animal it belongs to. Lions say 'Grr', Tigers say 'Rawr', Snakes say 'Ssss', and Birds say 'Chirp'. My code : sound = input() my_list = sound.split() if 'Grr' in sound : print(sound.replace('Grr', "Lion")) if 'Rawr' in sound : print(sound.replace('Rawr', "Tiger")) if 'Ssss' in sound : print(sound.replace('Ssss', "Snake")) if 'Chirp' in sound : print(sound.replace('Chrip', "Bird")) The problem is if the input is a mix like "Chrip Rawr" it can't replace both but it works fine when its just one type of sound Could you guys please help me with this? I would appreciate it alott

11th Sep 2020, 3:51 PM
pedram ch
pedram ch - avatar
7 Answers
+ 1
Yeah, its very close, but I dont think you need the replace function. You could probably also use a dictionary, but when I read the Task, the sounds all started with different letters, could probably also use the .startswith() method too...Try something like this https://code.sololearn.com/c6Uk19ass56i/?ref=app
11th Sep 2020, 4:01 PM
Steven M
Steven M - avatar
+ 2
Steven M method 3 was great I didn't think of using dictionary at all. Thank you so much
11th Sep 2020, 4:40 PM
pedram ch
pedram ch - avatar
+ 1
String are immutable in python.. So sound.replace('Grr', "Lion") can't change original string sound but make a new one by changes.. So if assign back like sound = sound.replace('Grr', "Lion") Now it changes... But you are printing 4 times, just print ones after replacing all..
11th Sep 2020, 4:16 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 thank you for the advice I tried this code but still it didn't word for one sample test: sound = input() if 'Grr' in sound : sound = sound.replace('Grr', "Lion") if 'Rawr' in sound : sound = sound.replace('Rawr', "Tiger") if 'Ssss' in sound : sound = sound.replace('Ssss', "Snake") if 'Chirp' in sound : sound = sound.replace('Chrip', "Bird") print(sound) I guess it still doesn't replace more than one of sounds I don't how to make it remember last replacement and also replace other one
11th Sep 2020, 4:43 PM
pedram ch
pedram ch - avatar
+ 1
pedram ch You have spelling mistakes in Chirp.. In replace.(), you wrote, Chrip instead Chirp...
11th Sep 2020, 6:22 PM
Jayakrishna 🇮🇳
+ 1
Jayakrishna🇮🇳 ohhh my bad thank you so much now its working
11th Sep 2020, 8:34 PM
pedram ch
pedram ch - avatar
+ 1
pedram ch you're welcome..
11th Sep 2020, 9:41 PM
Jayakrishna 🇮🇳