Function to validate the given credit card number. >>> validate_credit_card_number('4111111111111111') 4111111111111111 is a valid credit card number. True >>> validate_credit_card_number('helloworld$') helloworld$ is an invalid credit card number because it has nonnumerical
(credit_card_number: str)
| 53 | |
| 54 | |
| 55 | def validate_credit_card_number(credit_card_number: str) -> bool: |
| 56 | """ |
| 57 | Function to validate the given credit card number. |
| 58 | >>> validate_credit_card_number('4111111111111111') |
| 59 | 4111111111111111 is a valid credit card number. |
| 60 | True |
| 61 | >>> validate_credit_card_number('helloworld$') |
| 62 | helloworld$ is an invalid credit card number because it has nonnumerical characters. |
| 63 | False |
| 64 | >>> validate_credit_card_number('32323') |
| 65 | 32323 is an invalid credit card number because of its length. |
| 66 | False |
| 67 | >>> validate_credit_card_number('32323323233232332323') |
| 68 | 32323323233232332323 is an invalid credit card number because of its length. |
| 69 | False |
| 70 | >>> validate_credit_card_number('36111111111111') |
| 71 | 36111111111111 is an invalid credit card number because of its first two digits. |
| 72 | False |
| 73 | >>> validate_credit_card_number('41111111111111') |
| 74 | 41111111111111 is an invalid credit card number because it fails the Luhn check. |
| 75 | False |
| 76 | """ |
| 77 | error_message = f"{credit_card_number} is an invalid credit card number because" |
| 78 | if not credit_card_number.isdigit(): |
| 79 | print(f"{error_message} it has nonnumerical characters.") |
| 80 | return False |
| 81 | |
| 82 | if not 13 <= len(credit_card_number) <= 16: |
| 83 | print(f"{error_message} of its length.") |
| 84 | return False |
| 85 | |
| 86 | if not validate_initial_digits(credit_card_number): |
| 87 | print(f"{error_message} of its first two digits.") |
| 88 | return False |
| 89 | |
| 90 | if not luhn_validation(credit_card_number): |
| 91 | print(f"{error_message} it fails the Luhn check.") |
| 92 | return False |
| 93 | |
| 94 | print(f"{credit_card_number} is a valid credit card number.") |
| 95 | return True |
| 96 | |
| 97 | |
| 98 | if __name__ == "__main__": |
no test coverage detected