In: Computer Science
I'm trying to ask a series of questions with validation in python.
example:
What is your name? (if valid name then continue to question 2)
What is your email? (if not valid then ask this question again, if valid then next question)
How old are you? (again, if invalid then ask again, if valid then next.
import re
def get_new_artwork():
name_regex = "^(?=.{2,40}$)[a-zA-Z]+(?:[-'\s][a-zA-Z]+)*$"
email_regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
while True:
name = input('Enter your name: ').title().strip()
if re.search(name_regex, name) is None:
print('Invalid name. ')
else:
email = input('Enter your email: ')
if re.search(email_regex, email) is None:
print('Invalid email. ')
else:
age = int(input('How old are you? '))
if age >=17:
print('Too young. ')
else:
print('user added. ')
break
return (name, email, age)
get_new_artwork()
import re
def get_new_artwork():
name_regex = "^(?=.{2,40}$)[a-zA-Z]+(?:[-'\s][a-zA-Z]+)*$"
email_regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
while True:
name = input('Enter your name: ').title().strip()
if re.search(name_regex, name) is None:
print('Invalid name. ')
else:
while True: // added this to continuosly ask the user for valid email
email = input('Enter your email: ')
if re.search(email_regex, email) is None:
print('Invalid email. ')
else:
age = int(input('How old are you? '))
if age >=17:
print('Too young. ')
break // for breaking out of loop
else:
print('user added. ')
break
return (name, email, age)
get_new_artwork()
After breaking the above code, it meets the all requirements that is asked by the user in the question!!
i have make the valid changes!! and put comment aside!!
i hope you will like my response, and please don't forget to give thumbs up!!
if you have any query, do share in comment box!!
Stay safe and healthy!!
Thank you!!!