+ 5

[SOLVED] Code Coach: No Numerals. 1/6 Tests Failed.

#My Code txt = input() a = {'1':'one','2':'two', '3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine','0':'zero', '10':'ten'} for i in txt: if i in a: txt = txt.replace(i, a[i]) print(txt) #How to get all the results correct?

22nd Mar 2020, 7:09 AM
maf
maf - avatar
19 Respostas
+ 1
Edited works #Code Coach: No Numerals. 1/6 Tests Failed. #My Code txt = input() tx=txt.split(" ") a = {'1':'one','2':'two', '3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine','0':'zero','10':'ten'} for i in tx: if i in a: txt = txt.replace(i, a[i]) print(txt) #How to get all the results correct?
22nd Mar 2020, 7:44 AM
Justus
Justus - avatar
+ 2
maf you can try this also.. a=input("") d={"1":"one","2":'two',"3":'three',"4":'four',"5":'five',"6":'six',"7":'seven',"8":'eight',"9":'nine',"10":'ten',"0":'zero'} x=a.split(' ') for i in x: if i in d.keys(): u=x.index(i) x[u]=d[i] o=" ".join(x) print(o)
23rd Mar 2020, 5:35 PM
ANJALI SAHU
+ 2
šŸ™‚šŸ™‚
23rd Mar 2020, 6:24 PM
ANJALI SAHU
+ 2
numbers = { '0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten', } phrases = input() # i need 2 pumpkins and 3 apples result = '' for i in phrases.split(): result += (numbers.get(i, i) + ' ') print(result)
24th Mar 2020, 6:28 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Welcome sir
24th Mar 2020, 7:02 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
You forgot 10. There's test case that checks for number 10.
22nd Mar 2020, 7:16 AM
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ
ä½ ēŸ„é“č¦å‰‡ļ¼Œęˆ‘也ę˜Æ - avatar
+ 1
Use reg ex import re txt = input() a = {'1':'one','2':'two', '3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine','10':'ten', '0':'zero'} new_txt = re.sub('(\d+)', lambda m: a[m.group()], txt) print(new_txt)
22nd Mar 2020, 7:35 AM
Ore
Ore - avatar
+ 1
Using replace() function for each character won't work for example, if the string is '10 times better' replace() will transform it to 'onezero times better' because it works at character level. That is why you need regular expressions https://www.sololearn.com/learn/JUMP_LINK__&&__Python__&&__JUMP_LINK/2475/
22nd Mar 2020, 7:40 AM
Ore
Ore - avatar
+ 1
Justus thanku somuch, that works too :)
22nd Mar 2020, 7:48 AM
maf
maf - avatar
+ 1
RAJESH SAHU Ohh thats nice too, thnx
23rd Mar 2020, 6:04 PM
maf
maf - avatar
23rd Mar 2020, 11:38 PM
Syed Waseem
Syed Waseem - avatar
0
CarrieForle lol i thought that too, but no I tried that. I have it in my code, i edited it out here in qs coz i thought 10 was not included xD
22nd Mar 2020, 7:18 AM
maf
maf - avatar
0
Ore Adeleye man thats pretty advanced xDD, can u please explain me that what was wrong in my code? And thanks to both for trying to help.
22nd Mar 2020, 7:36 AM
maf
maf - avatar
0
Ore Adeleye oooooh, thnku so much
22nd Mar 2020, 7:40 AM
maf
maf - avatar
0
Code Crasher thanks for explaining, Justus's way (similar to yours) was simpler and I got it easily, Ore Adeleye's way was more advanced.
22nd Mar 2020, 9:24 AM
maf
maf - avatar
0
Code Crasher lol yeah u r right. :) after this ima learn some advanced stuff.
22nd Mar 2020, 9:44 AM
maf
maf - avatar
0
24th Mar 2020, 6:55 AM
maf
maf - avatar