Simple character validation.
Watch this video to learn about the new concepts shown in the program:
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).
Questions to think about with this program to check your understanding:
Identify the Boolean variable used to flag if a character is not a number.
valid is a Boolean variable used to flag if a character is not a number.
State what data type is before the dot in character.isdigit() in line 11.
“character” is a string data type, it just happens to only contain one character. Some languages support a character data type.
Change the program so that it:
validate_name to check whether the name only contains alphabet characters or spaces and outputs the name in upper case.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.
validate_number
# Function to validate the number is numbers only
def validate_number(number_on_card):
---
valid = True
number_of_characters = len(number_on_card)
---
# Must be 16 (without spaces) or 19 characters (with spaces)
if number_of_characters == 16 or number_of_characters == 19:
---
# Check each character in the number
for character in number_on_card:
---
# Anything other that a digit and space is invalid
if not character.isdigit() and character != " ":
---
valid = False
---
else:
---
valid = False
---
return valid
validate_name
# Function to validate the name is letters and space only
def validate_name(name_on_card):
---
valid = True
---
# Check each character in the name
for character in name_on_card:
---
# Anything other than an alphanumeric or a space is invalid
if not character.isalpha() and character != " ":
---
valid = False
---
return valid
input_card_details
# Input valid name and number for a debit card (not using check digits)
def input_card_details():
---
valid_card = False
---
# Only progress if the name is valid
while not valid_card:
---
name_input = input("Enter the name on the card: ")
---
valid_card = validate_name(name_input)
---
valid_card = False
---
# Only progress if the number on the card is valid
while not valid_card:
---
number_input = input("Enter the 16 digit number: ")
---
valid_card = validate_number(number_input)
---
print(name_input.upper())