0

how can we change this program?

how can we change this program to print if it has any numbers in string it has to print "please enter without numbers"? if the string dont have anynumbers it has to print the string. eg : this is 2 good output : please enter without numbers https://code.sololearn.com/cd8IS585n71g/?ref=app

29th Jul 2020, 5:03 AM
Mr. 12
Mr. 12 - avatar
7 Respuestas
+ 3
b is a pattern to check if the given string contains any numbers (from 0 to 9). re.search is a function checking if "b", the pattern is within "a". Thus, "if not re.search(b,a)" is True, if and only if "a" doesn't contains any number.
29th Jul 2020, 5:17 AM
Panko
Panko - avatar
+ 2
import re a = input("enter a string only: \n") b = "[0-9]" if not re.search(b,a): print(a) else : print("Please enter without numbers.")
29th Jul 2020, 5:08 AM
Panko
Panko - avatar
+ 1
s = input() print('please input without numbers' if sum(c.isdigit() for c in s) else s)
29th Jul 2020, 6:08 AM
Bilbo Baggins
Bilbo Baggins - avatar
+ 1
Sure! (c.isdigit() for c in s) is called generator as you already maybe know. To see its effect add a print(list(c.isdigit() for c in s)) For example: Solo1 -> [ False False False False True ], or [0 0 0 0 1] sum() will add all the elements of the list above, and it will give a result > 0 if some digit is present if sum(...) means: if sum(...) not null the construct <things todo> if ... else <other things to do> is a shortcut of if ...: <things to do> else: <other things to do> in this case <things to do> are really the argument to print: 'please input without numbers' or the string s itself Happy pythoning!
29th Jul 2020, 7:07 AM
Bilbo Baggins
Bilbo Baggins - avatar
0
Panko Thank You can you explain that program?
29th Jul 2020, 5:10 AM
Mr. 12
Mr. 12 - avatar
0
ok i got it
29th Jul 2020, 5:14 AM
Mr. 12
Mr. 12 - avatar
0
Bilbo Baggins can you explain me your program? why did you use sum there!?
29th Jul 2020, 6:40 AM
Mr. 12
Mr. 12 - avatar