Question

In: Computer Science

I'm trying to ask a series of questions with validation in python. example: What is your...

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()

Solutions

Expert Solution

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!!!


Related Solutions

I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a...
I'm trying to use Jupyter (python) to convert the contents of a pkl file into a dictionary, WITHOUT using pandas. I'm able to import pickle and can open my file...but I'm not sure how to create the dictionary.
This is for Python programming, and I am trying to use 2 for loops to ask...
This is for Python programming, and I am trying to use 2 for loops to ask a slaesperson how many of 5 different items they sold, then the program is to calculate the total dollar amount sold. Formatting and all that aside, I am having an issue with the loops not stepping through the 2 lists at the same time. The first loop is taking all 5 entered quantities times the price of the first item, and then all 5...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...
Invalid entry code in python my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit" import math user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>: 1 sin(x) 2 cos(x) 3 tan(x) 4 asin(x) 5 acos(x) 6 atan(x) 7 ln(x) 8...
Hi, I'm trying to make a basic GUI quiz in python using tkinter. I would like...
Hi, I'm trying to make a basic GUI quiz in python using tkinter. I would like the user to have to press a start button to begin the quiz and then be asked 5 different questions with the total score showing at the end. Ideally it would be a multiple choice quiz with the user having 4 different answers to choose from. A very basic example of this would be appreciated so I can understand where exactly I'm going wrong....
I'm trying to explain in simple terms what the weak interaction does, but I'm having trouble...
I'm trying to explain in simple terms what the weak interaction does, but I'm having trouble since it doesn't resemble other forces he's familiar with and I haven't been able to come up (or find on the web) with a good, simple visualization for it.
Using Python and Kivy to create a App to ask users 5 questions with Yes or...
Using Python and Kivy to create a App to ask users 5 questions with Yes or No button. 1. Are you older than 14? 2. Did you pass 8th or 9th Grade? 3. Are you ready gor highschool? 4. Do you have 4 more years to complete highschool? 5. Are you homeschool? If the users click Yes for 3 questions or more than you should get a results page saying this users is in highschool or else the user is...
I'm working on python code which is substring matching. For example, the given string is input...
I'm working on python code which is substring matching. For example, the given string is input "CTTGTGATCTCGTGTCGTGGGTAG", and a substring we want to find in the main one is "GTGG". So the code will print out the position and the substrings in the main which has exactly one different position. For example, start at position 4 the substring is GTGA since there is only one letter different, and so on. Even though we know that string index starts at 0,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT