-1
file = open('Name.txt', 'r')
name =  str(file.read())
file.close()

In the file called 'Name.txt', the user will input a name. The program is to check whether or not the name is valid.

E.g:

1bob - is not valid

bob - is valid

Is it possible to do this with an if condition?

1
  • 2
    The requirements for your validation are not clearly stated. And how have you attempted to solve this? The code you posted is almost not related to the problem Sep 17, 2016 at 9:04

2 Answers 2

2
def validate(my_file):
    return not any(x.isdigit() for x in open(my_file).read().strip())

The above function will return True when name is valid else False.

-1

You could use regular expression :

i.e :

>>> import re
>>> regex = re.compile(r'.*[0-9]')
>>> names = ['Bob', 'John', '1Bob', 'John2']
>>> for name in names: 
...     if not regex.match(name):
...          print name
... 
Bob
John

.* matches any character (except newline)

Quantifier: * Between zero and unlimited times

[0-9] match a single character present in the list below

0-9 a single character in the range between 0 and 9

Hope it helps.

2
  • Thanks. Exactly what I was looking for.
    – Lord Heffe
    Sep 17, 2016 at 9:28
  • Please, if somebody down votes, please tell my why. If my answer is not good, I'd like to know why. Thank you.
    – JazZ
    Sep 17, 2016 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.