0

Still can't tell the reason why this code failed 1 test case

num_words ={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine','10':'ten'} phrase = input() new_phrase ='' for i in phrase: if i in num_words.keys(): new_phrase +=num_words[i] else: new_phrase +=i print(new_phrase )

10th May 2025, 4:40 PM
Victor Uwemedimo George
3 Respuestas
+ 5
Victor Uwemedimo George , > the problem with this code is that it compares the input string character by character against the dictionary keys, this does not work correct in all cases. > to fix this issue, we can split the input string into words (at spaces positions), then check each word against the dictionary by using a loop. > it is also better to build the new phrase as a list of words and join them at the end with spaces.
10th May 2025, 7:52 PM
Lothar
Lothar - avatar
+ 4
Enter "10 bees". Your code will output "onezero bees".
10th May 2025, 5:12 PM
Lisa
Lisa - avatar
+ 1
thanks a lot
11th May 2025, 9:09 AM
Victor Uwemedimo George