Simple character validation.
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:
The new commands used in this program and others that may be useful. Select them below to learn more:
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.
Returns True if string x is in upper case or False if not.
Returns True if string x is in lower case or False if not.
Returns True if string x only contains alphabet letters a-z or A-Z.
Returns True if string x only contains numbers 0-9.
Returns True if string x only contains alphabet letters or numbers.
x is assigned to be the lower case of y.
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:
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.
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.
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.