+ 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
9 Respuestas
+ 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()])
+ 2
Evgeny Sergeev Instead of the entire inner for loop (for k ...). Just print integer.get(i,i) directly.
+ 2
Make sure it does not turn "10" into "onezero".
+ 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))
+ 1
for i in phrase.split():
0
Mustafa A dont understand where to insert it? Instead of integer[k]?
0
Mustafa A cant get it work, doesnt work
0
Mustafa A 1 case fails out of 6
0
Evgeny Sergeev Change phrase to "phrase.split()".