+ 2

No numerals

It need to take only 0-10 numbers and replace them with number names as a string How do I exclude double digits without using Regex ? My code: phrase = input() integer = { "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "10": "ten" } for i in phrase: for k in integer: if i == k: x = phrase.replace(i, integer[k]) print(x) #3 is a crowd => three is a crowd

7th Apr 2022, 9:53 PM
Evgeny Sergeev
Evgeny Sergeev - avatar
9 odpowiedzi
+ 2
integer.get(i, i) Will get the value associated with the key "i" or "i" if the key doesn't exist. Also, the nicer and simpler way of solving this is to use list comprehension. Something like " ".join([integer.get(word, word) for word in phrase.split()])
7th Apr 2022, 10:10 PM
Mustafa A
Mustafa A - avatar
+ 2
Evgeny Sergeev Instead of the entire inner for loop (for k ...). Just print integer.get(i,i) directly.
8th Apr 2022, 12:47 AM
Mustafa A
Mustafa A - avatar
+ 2
Make sure it does not turn "10" into "onezero".
8th Apr 2022, 1:05 AM
Simon Sauter
Simon Sauter - avatar
+ 1
Evgeny Sergeev phrase = input() integer = { "0": "zero", "1": "one", "2": "two", "3": "three", "4": "four", "5": "five", "6": "six", "7": "seven", "8": "eight", "9": "nine", "10": "ten" } for i in phrase: print(integer.get(i,i))
8th Apr 2022, 12:58 AM
Mustafa A
Mustafa A - avatar
+ 1
for i in phrase.split():
8th Apr 2022, 1:18 AM
Rik Wittkopp
Rik Wittkopp - avatar
0
Mustafa A dont understand where to insert it? Instead of integer[k]?
7th Apr 2022, 10:41 PM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
Mustafa A cant get it work, doesnt work
8th Apr 2022, 12:56 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
Mustafa A 1 case fails out of 6
8th Apr 2022, 1:00 AM
Evgeny Sergeev
Evgeny Sergeev - avatar
0
Evgeny Sergeev Change phrase to "phrase.split()".
8th Apr 2022, 1:18 AM
Mustafa A
Mustafa A - avatar