0
I want to solve No numerals code coach in python.But I am struggling here.how to replace the number with in words?
sen=input() sen=sen.lower() dict={0:"zero",1:"one",2:"two",3:"three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine"} for i in sen : if i in dict[ i in range(0,10)]: sen=sen[i].replace(dict.get(i)) print(sen) #I think I am not clear about using dictionary functions here.pls suggest me.Here my error is string indices must be integer.I can'table to understand this error.If any other solutions if you have that also welcomed.sry for bad English.
2 Réponses
+ 1
Or what I've done is write a list with numbers written out in order. The index is then correct. You split string, then convert the number to an integer and use this to call out the correct written number from your written list. I.e. x=0 print(list[x]) outputs zero
Then you use the position of X to insert it back with the rest of your split string (after removing the number from its position). Use an empty string to add/combine each word from your now accurate split string into one line and done!
+ 3
for i in sen:
Will iterate over each character in the string. You need to split() the string into a list of words first.
Then you can check if the word isnumeric()
If yes then access the dictionary using the word as the key (don't forget to convert it to an int()) like dict[key] to get the string value of the number and use that value to replace the number in the list of words.
When done join() the list of words with a space and print() it