Debit card

Debit card

Simple character validation.

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

Command summary

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


Success Criteria

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.

Help

Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.

Evaluate


Run your code to check that your program has met the success criteria and produces the required output.

Enter the name on the card: Cra1g Sargent

Enter the name on the card: Craig Sargent

Enter the 16 digit number: 5555666677778888

CRAIG SARGENT

Card details valid.

Enter the name on the card: D@ve Hillyard

Enter the name on the card: Dave Hillyard

Enter the 16 digit number: 1111 2222 3333 4444 5555 6666

Enter the 16 digit number: 1111222233334444555f

Enter the 16 digit number: 11112222333344445555

Enter the 16 digit number: 111122223333444f

Enter the 16 digit number: 1111222233334444

DAVE HILLYARD

Card details valid.

Registered in England and Wales: 10442992

VAT Number: 290 9845 58

Telephone: 01452 947500

Email: admin@craigndave.co.uk

Craig'n'Dave logo

Craig 'n' Dave

Bett Awards 2024 Finalist