+ 1

Why wont this work for all cases? This code is supposted to take a str and replace the numbers with their written form.

def numeral_to_word(txt): nya = "" is_number = "0 1 2 3 4 5 6 7 8 9 10" equivalent = {0: "zero", 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eigth", 9: "nine", 10: "ten"} for char in txt: if char in is_number.split(" "): nya += equivalent[int(char)] else: nya += char print(nya) numeral_to_word(input())

21st Jul 2022, 7:18 PM
rainhead
rainhead - avatar
2 Réponses
+ 4
IF 10 is a needed number, then fix that. With your code it translates to "onezero"
21st Jul 2022, 7:43 PM
Slick
Slick - avatar
+ 1
Anton Ranhed the code needs to check the whole input word. By checking one character at a time, it fails to recognize the two-character string "10". Beware that merely adding a two-character check won't be enough either. It needs to let "100" pass through and not mistakenly translate it as "tenzero". Also check the spelling. One of your number strings is misspelled, which would be another reason it would not pass the tests.
22nd Jul 2022, 4:43 AM
Brian
Brian - avatar