+ 1

No numerals, code coach

I have been trying to solve the no numerals code coach problem using python. This is the code i cameup with but it doesn't work. I cant figure out why. Will someone help? I dont need the solution for the problem. I just need an explanation as to why it wont work. Im a beginner with python btw s=input() nums=["zero","one","two","three","four","five","six","seven","eight","nine","ten"] for i in range(11): s.replace(str(i),nums[i]) print(s)

10th Feb 2020, 1:14 PM
Salman Nazeer
Salman Nazeer - avatar
7 Réponses
+ 5
You need to restore your new string in s. s = s.replace(str(i),nums[i]) There is one more problem after that: Try to run your code with 10 and see what happens. 😉
10th Feb 2020, 1:22 PM
HonFu
HonFu - avatar
+ 5
A String is immutable. So replace() returns another String and s keeps the same. Assign the return of replace to s.
10th Feb 2020, 1:24 PM
Oma Falk
Oma Falk - avatar
+ 5
I'm hunting it right now. Thank you guys for that valuable information! and thanks for helping out a beginner.
10th Feb 2020, 1:27 PM
Salman Nazeer
Salman Nazeer - avatar
+ 4
Salman Nazeer u did a good job 👏👏
10th Feb 2020, 1:28 PM
Oma Falk
Oma Falk - avatar
+ 2
Found the bug. Onezero. Thanks for pointing me in the right direction HonFu and Oma. I appreciate you guys giving me an oppurtunity to learn from my mistakes.
10th Feb 2020, 1:32 PM
Salman Nazeer
Salman Nazeer - avatar
+ 2
I just added s=s.replace('onezero','ten') when the program exited the loop. I know its not a great fix but i just fit it in
10th Feb 2020, 2:28 PM
Salman Nazeer
Salman Nazeer - avatar