How to validate name and age

Hi! I need help with my code. I’m trying to validate user input but I don’t know how to validate the name using a string so they can only enter strings and not numbers. As for the age I’m trying to make input only numbers and not strings, and if it is less than 10 it should say that its too young. Can someone help?

image

To validate that a string meets your requirements you can use a regex pattern. Assuming you want to accept a name like “John Smith-Carter” but not “50 Cent” you could use a pattern like:

r'^(?:[A-Z][a-z]+[-\s]?)+$'

The above pattern will accept any string that starts with a capital letter followed by one or more lowercase letters followed by an optional whitespace character or - . It will accept any number of repetitions of that pattern.

To verify that you have an integer number as the input you could use a regex pattern (r'^\d+$') or try converting it to int:

try:
    int(my_string)
except ValueError:
    <Code for handling a non-integer value>

Once you have converted the age to be an int you use a simple age <= 10 check for the “is old enough” part of the verification.

I hope this helps.

1 Like

To get the age, use this:

age = int(input("Please enter your age: "))
if type(age) != int:
    age = int(input("Please enter a valid age: ")

Remember that the input keyword only accepts strings. So, you have to change the str string input into an int integer type (i.e., type casting).

When making the comparison just below:

if age <= 10:
    print("You are too young!")
    age = int(input("Please enter a valid age: ")
1 Like

When validating a name, be sure not to exclude any actually valid names. Here’s a bit of a list of things you might not think of:

2 Likes

You should always call int('string') in a try/except because you need to catch the ValueError it will thrown when someone inputs ten.

1 Like

I agree with you 100% especially when you’re expecting user input as there is always a chance for an input error. However, from the looks of it, I think that this might be an academic exercise (from the nature of the question); and exception handling is usually not covered until much, much later (at least in my case).

3 Likes

input gives you a string in Python 3.x. Full stop.

Strings can contain 0, 19 symbols in them. A string is not an integer even if it contains only those symbols.

An integer does not contain those symbols. It doesn’t “contain” anything, really. It represents a single numeric value. Symbols like 0 and 9 are only a part of text that we use to show a number, so that we can read and understand it. They are not actually the number itself. (And there are countless ways to “write a number”, which don’t all use those symbols.)

Whenever we write out some text (e.g., display it on the screen), we again have a string. When you print a number, it gets converted to a string first.

== only means “equal to”. != only means “not equal to”[1]. Things are not equal to their types: an integer is not “equal to integer”. (Just like how I am a human, but I am not “equal to human” - that doesn’t make any sense.)

Please read:


  1. This is not the place for pedantry about Numpy’s special definitions, please. ↩︎

1 Like