How do i validate a user input in python ? I am writing this code , the user has to put in their name, and it has to be two names separated by a space and the name should contain only letters
def Name():
n = 0
name = input('name please\n>>')
c = name.split(' ')
while len(c) != 2:
print('we need two names')
name = input('name please\n>>')
c = name.split(' ')
while True:
for i in name:
if i not in r:
n+=1
if n > 0:
print('Letters only')
else:
break
Name()
Use .isalpha()
and I suggest just doing this recursively.
def Name():
name = input('name please\n>>')
c = name.split(' ')
if len(c) != 2:
print('we need two names')
Name()
for item in c:
if not item.isalpha():
print('Letters only')
Name()
return name
Name()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments