+ 2

Code Not Correct

https://code.sololearn.com/c0ril2MdBaCc/?ref=app I want to select out all the digits from a sentence and coded the program but don't know what is going wrong with this. Can someone hep me out with this. P.S. : I've edited the program and now it's running just fine

31st May 2019, 8:28 AM
Prateek Bhardwaj
Prateek Bhardwaj - avatar
3 ответов
+ 1
But it works fine. But you can just convert the char to int before appending to avoid all the quotes. #python 3.7.1 def NumExt(words): numlist = list() for i in words: for char in i: if char.isdigit(): numlist.append(int(char)) return numlist sentence = input() words = sentence.split(" ") print(NumExt(words))
31st May 2019, 4:45 PM
Ojabuoma Miracle Iyanu
Ojabuoma Miracle Iyanu - avatar
+ 10
# try this- def NumExt(words): numlist = [] for i in words: try: val = int(i) numlist.append(val) except ValueError: continue return numlist str = "1solve5d7" print(NumExt(str)) # output: [1,5,7]
31st May 2019, 8:40 AM
VEDANG
VEDANG - avatar
+ 5
A little more compact, using regex: import re ptrn = re.compile(r'(\d)') s = 'sghs23sa8e43aajhsj8' print(list(map(int, ptrn.findall(s))))
31st May 2019, 12:13 PM
Anna
Anna - avatar