Exercise 12-08:

Write a program that validates user data entry. It will validate ISSNs (International Standard Serial Numbers), which are used to identify periodical publications, like magazines. The program must accept ISSNs in the following format: ISSN 9999-9999, where each 9 represents a digit. Require the dash at the end, verifying the correct number of digits.
The word ISSN is optional and must be accepted regardless of the case. If the word ISSN is specified, the space between it and the number is required.

Valid values :
ISSN 1234-45678
1234-45678

Invalid values:
ISSN 123-4567
IssN11112-22

Answer:

##############################################################################
# Python From Scratch
# Author: Nilo Ney Coutinho Menezes
# Editora Novatec (c) 2010-2025 - LogiKraft 2025
# Site: https://pythonfromscratch.com
# ISBN: 978-85-7522-949-1 (Paperback), 978-85-7522-950-7 (hardcover), 978-85-7522-951-4 (ebook)
#
# File: chapter 12/exercise-12-08.py.py
##############################################################################
# Functions number, sequence and check pattern from listing 12.5
import re

CNPJ_RE = r"^\d{2}\.\d{3}\.\d{3}/\d{4}-\d{2}$"


# Checks if input is a valid CNPJ, that is:
# A sequence in the format 99.999.999/9999-99
def cnpj(input):
    return bool(re.match(CNPJ_RE, input))


inputs = [
    "12.345.678/9012-34",  # Yes
    "12.345.678-9012-34",  # No
    "12.345.678.9012-34",  # No
    "2.345.678/9012-34",  # No
    "12.345.678/9012-4",  # No
    "99.999.999/9999-99",  # Yes
]

for input in inputs:
    print(f"{input}: is it a CNPJ? {'Yes' if cnpj(input) else 'No'}")
Click here to download the file