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 )
3 Antworten
+ 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.
+ 4
Enter "10 bees". Your code will output "onezero bees".
+ 1
thanks a lot