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
7 Réponses
+ 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.
+ 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.")
+ 1
s = input()
print('please input without numbers' if sum(c.isdigit() for c in s) else s)
+ 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!
0
Panko Thank You
can you explain that program?
0
ok i got it
0
Bilbo Baggins can you explain me your program?
why did you use sum there!?