Please help me understand what is wrong with this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Please help me understand what is wrong with this code

txt = "7, 5 4 4 3 2 2 1 7 88 8. 5 3 3 ubs uguwbb hgajdbihw. hbwhedb 10 19 7 9 8 5 3" dict = {"10" : "ten", "0": "zero", "1" : "one", "2" : "two", "3" : "three", "4" : "four", "5" : "five", "6" : "six", "7" : "seven", "8" : "eight", "9" : "nine", } x = -1 for i in txt: x += 1 if str(txt[x]).isdigit: try: print(float(txt[x] + txt[x + 1])) except ValueError: txt = txt.replace(txt[x], dict[txt[x]]) else: continue print(txt) __________________________________________________________________________________________________________________________ I wanted to do the "no numerals" challenge where I need to change the numbers 0-10 with their names and the rest as digits. Suppose the text is: "7 is an odd number while 4 and 6 are even 9, on the other hand is odd. 24 times 2 is 48" The result should be: "seven is an odd number while four and six are even nine, on the other hand is odd. 24 times two is 48" I want to do this in python

15th May 2021, 2:59 PM
Arnab Das
Arnab Das - avatar
12 Answers
+ 2
Why don't you split your text and check each word?
15th May 2021, 3:27 PM
Rohit
+ 2
Are you worried for about something like "41,"?
15th May 2021, 4:21 PM
Rohit
15th May 2021, 5:20 PM
Rohit
+ 1
Oh yes
15th May 2021, 4:22 PM
Arnab Das
Arnab Das - avatar
+ 1
import re a = "7 41, arnab23" for w in a.split(): print(*re.findall(r'[0-9]+', w))
15th May 2021, 4:25 PM
Rohit
+ 1
Did you try to run it once? Anyway, [0-9]+ is a regex(a regex is used for find patterns in a string). The pattern we want is sequence of digits from a string and for that we added this "[0-9]+" ( [0-9]+ matches one or more digits such as '123', '000') for example, for a string "arnab266" the regex would return 266 ignoring all the letters like isdigit() does.
15th May 2021, 4:35 PM
Rohit
+ 1
Arnab Das In the challenge No numerals, if I'm not wrong there such inputs like "44," or number than 10. So this code much work fine. However, if want to make a more dynamic one like where handles comma or any characters or for large number like 88 etc then I think you need to learn more and add extra thing like for example regex or some more basic string algorithms to built it. https://code.sololearn.com/c1aLhmXzqvH0/?ref=app
15th May 2021, 4:45 PM
Rohit
+ 1
I think I need to learn more... Thanks!
15th May 2021, 4:48 PM
Arnab Das
Arnab Das - avatar
0
Sorry, I am very new to programming but I tried that, but then if the digits have commas after them, then the element will have commas with the numbers. Now, I tried to do that by removing the commas and then changing them and....... It creates a mess which is very hard for me to understand. Python is my first language, so I don't know python completely. Thanks for replying.... Waiting for another Can you please tell me why the try and except statement are not working? It still shows value error
15th May 2021, 4:04 PM
Arnab Das
Arnab Das - avatar
15th May 2021, 4:19 PM
Arnab Das
Arnab Das - avatar
0
Sorry I am a very beginner so I am not sure what this code means. Could you please explain it to me?
15th May 2021, 4:28 PM
Arnab Das
Arnab Das - avatar
0
Yes I ran it. I actually don't know anything about the python modules, so I tried to code it with the inbuilt functions. Isn't there no way to do it without using any modules?
15th May 2021, 4:40 PM
Arnab Das
Arnab Das - avatar