Skip to content
Advertisement

How to validate multiple “if” statements in Python from a user’s input [closed]

The task I need to do is the following;

  • Create a Python file called full_name.py
  • This program will be used to validate that a user enters inputs at least two names when asked to enter their full name.
  • Ask the user to input their full name.
  • Do some validation to check that the user has entered a full name. Give an appropriate error message if they haven’t. One of the following messages should be displayed based on the user’s input:
    • “You haven’t entered anything. Please enter your full name.”
    • “You have entered less than 4 characters. Please make sure that you have entered your name and surname.”
    • “You have entered more than 25 characters. Please make sure that you have only entered your full name.”
    • “Thank you for entering your name.”

This is the code that I have thus far;

full_name = str(input("Please enter your full name below:n"))

if (full_name.isalpha()):
    print ("You haven't entered anything. Please enter your full name")

I am not sure if this is 100% correct, but I am also not sure how to continue from here?

Advertisement

Answer

To check the length of the input you could use the len() function, length of 0 means the user hasn’t entered anything

for example for input under 4 characters:

    if len(full_name ) < 4:
        print("You have entered less than 4 characters")

Same but different parameters would be for the rest.

for checking if the name has two different parts, split by a whitespace. you can split the name by whitespace and check if you have two parts.

parts = full_name.split(' ')
if len(parts) == 2:
    // name has two parts
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement