+ 2

Just wondering what is keeping this code from working for the no numerals problem.

words = input() numbers = ['1','2','3','4','5','6','7','8','9','0'] numbers2 = {'1':'one', '2':'two','3':'three','4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine', '0':'zero'} for x in words: if x in numbers: words.replace(x, numbers2[x]) else: continue print(words)

16th Mar 2022, 5:36 AM
Nick Accordino
Nick Accordino - avatar
3 odpowiedzi
+ 5
Nick Accordino , you need to use split() to separate the input string to individual words. otherwise you will get results like this: input: "hello 1 2 here is number 1234 000 1010" will result in: "hello one two here is number onetwothreefour zerozerozero onezeroonezero" so you can see, that using replace can not be an option.
16th Mar 2022, 11:54 AM
Lothar
Lothar - avatar
+ 4
Hi Nick! Another thing that you need to consider is replace method has a return value which is string meaning it doesn't update the original value itself. So, you need to assign with a new value using same variable or anything. words = words.replace() Also, you can use split() to handle numbers like 10 since it's a two digit number.
16th Mar 2022, 5:54 AM
Python Learner
Python Learner - avatar
+ 1
What happens when the number is 10? {Edit: these are the instructions. You write a phrase and include a lot of number characters (0-9), but you decide that for numbers 10 and under you would rather write the word out instead. Can you go in and edit your phrase to write out the name of each number instead of using the numeral? Task: Take a phrase and replace any instances of an integer from 0-10 and replace it with the English word that corresponds to that integer. } EDIT2 I think it is pretty crappy including 10 in the logic, but that is what they want. Although they say 0-9 the text says 10 and under.
16th Mar 2022, 5:44 AM
HungryTradie
HungryTradie - avatar