Simple character validation.

Debit card

⭐⭐⭐

Try

Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

The new commands used in this program and others that may be useful. Select them below to learn more:

x = len(y)

x is assigned to be the number of characters in string. Can also be used to return the number of items in an array/list.

x.isupper()

Returns True if string x is in upper case or False if not.

x.islower()

Returns True if string x is in lower case or False if not.

x.isalpha()

Returns True if string x only contains alphabet letters a-z or A-Z.

x.isdigit()

Returns True if string x only contains numbers 0-9.

x.isalnum()

Returns True if string x only contains alphabet letters or numbers.

x = y.lower()

x is assigned to be the lower case of y.

x = y.upper()

x is assigned to be the upper case of y (capital letters).

Investigate

Questions to think about with this program to check your understanding:

Item question

Identify the Boolean variable used to flag if a character is not a number.

Reveal answer

valid is a Boolean variable used to flag if a character is not a number.

Structure question

State what data type is before the dot in character.isdigit() in line 11.

Reveal answer

“character” is a string data type, it just happens to only contain one character. Some languages support a character data type.

Make

Change the program so that it:

  1. Asks the user to enter their name before their card number.
  2. Has an additional function called validate_name to check whether the name only contains alphabet characters or spaces and outputs the name in upper case.

Typical inputs and outputs from the program would be:

Enter the name on the card: john smith
Enter the 16 digit number: 1234 1234 1234 1234
JOHN SMITH
Card details valid.

Enter the name on the card: invalidname2342
Enter the name on the card: j@hn sm!th
Enter the name on the card: john smith
Enter the 16 digit number: 1234 1234 1234 1234
JOHN SMITH
Card details valid.