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-4567
1234-4567

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
##############################################################################
import re


def validate_issn(issn):
    # Pattern explanation:
    # ^ - start of string
    # (?:ISSN\s+)? - optional "ISSN" followed by one or more spaces
    # \d{4}-\d{4} - exactly 4 digits, dash, exactly 4 digits
    # $ - end of string
    pattern = r"^(?:ISSN\s+)?\d{4}-\d{4}$"

    # Check if the input matches the pattern (case-insensitive)
    return re.match(pattern, issn, re.IGNORECASE)


print("ISSN Validator")
print("=" * 50)
print("Enter ISSN numbers to validate.")
print("Valid formats: 'ISSN 1234-5678' or '1234-5678'")
print("Press Enter without input to exit.")
print()

while True:

    issn_input = input("Enter ISSN: ").strip()

    # Exit condition
    if not issn_input:
        print("Goodbye!")
        break

    # Validate the ISSN
    if validate_issn(issn_input):
        print("✓ VALID ISSN")
    else:
        print("✗ INVALID ISSN")

    print()
Click here to download the file