Help with no_numerals code challenge (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with no_numerals code challenge (Python)

So i finished my first code challenge today and am very happy about it. The challenge was to write a code that swaps out numbers from 0 to 10 for their respective number words, e.g 0 for zero. Even tho i could solve it I'm not quite happy with it, since I'm sure there are smarter ways to check for a 10 in a sentence, maybe with some smart re? Your ideas are very much appreciated :) Code below import re num_dic = { '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '0': 'zero', '10': 'ten', } pattern = r"^[0-9]

quot; phrase = input('Insert a phrase to substitute the numerals: ') new_str = '' new_list = phrase.split() i = 0 for word in phrase.split(): if re.match(pattern, word) or word == '10' : new_list[i] = num_dic[word] i += 1 new_str = ' '.join(new_list) print(new_str)

24th Apr 2020, 2:40 PM
Nico Bergner
Nico Bergner - avatar
1 Answer
+ 2
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)
24th Apr 2020, 2:53 PM
ANJALI SAHU