Debug an ISBN Validator - Debug an ISBN Validator

Tell us what’s happening:

Hello,
I’ve been having trouble debugging this code and I’ve searched a lot on the forum and the Internet. This is what I eventually got. I ran it in VS code and it worked perfectly and always gave the desired output. But on the website the same code somehow gives totally different outputs and doesn’t match the tests.
What is the problem and what should I do?

Your code so far

def validate_isbn(isbn, length):

    if len(isbn) != length:
        print(f"ISBN-{length} code should be {length} digits long.")
        return

    main_digits = isbn[:length - 1]
    given_check_digit = isbn[length - 1]

    try:
        main_digits_list = [int(digit) for digit in main_digits]
    except ValueError:
        print("Invalid character was found.")
        return

    if length == 10:
        expected_check_digit = calculate_check_digit_10(main_digits_list)
    else:
        expected_check_digit = calculate_check_digit_13(main_digits_list)

    if given_check_digit == expected_check_digit:
        print("Valid ISBN code.")
    else:
        print("Invalid ISBN code.")


def calculate_check_digit_10(main_digits_list):

    digits_sum = 0

    for index, digit in enumerate(main_digits_list):
        digits_sum += digit * (10 - index)

    result = 11 - digits_sum % 11

    if result == 11:
        expected_check_digit = '0'
    elif result == 10:
        expected_check_digit = 'X'
    else:
        expected_check_digit = str(result)

    return expected_check_digit


def calculate_check_digit_13(main_digits_list):

    digits_sum = 0

    for index, digit in enumerate(main_digits_list):
        if index % 2 == 0:
            digits_sum += digit * 1
        else:
            digits_sum += digit * 3

    result = 10 - digits_sum % 10

    if result == 10:
        expected_check_digit = '0'
    else:
        expected_check_digit = str(result)

    return expected_check_digit


def main():

    user_input = input("Enter ISBN and length: ")

    if "," not in user_input:
        print("Enter comma-separated values")
        return

    values = user_input.split(",")

    
    if len(values) != 2:
        print("Enter comma-separated values")
        return

    myisbn = values[0]
    mylength = values[1]

   
    if not mylength.isdigit():
        print("Length must be a number.")
        return

    mylength = int(mylength)

   
    if mylength != 10 and mylength != 13:
        print("Length should be 10 or 13.")
        return

    
    valid_chars = "0123456789X"

    for char in myisbn:
        if char not in valid_chars:
            print("Invalid character was found.")
            return

    validate_isbn(myisbn, mylength)


#main()

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36

Challenge Information:

Debug an ISBN Validator - Debug an ISBN Validator

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-isbn-validator/686b9720ee1d032bd77a480a.md at main · freeCodeCamp/freeCodeCamp · GitHub

Welcome to the forum @somebodysomeone,

What are you doing to test your code?

Also, the previous theory lecture was about understanding error handling. I don’t see you using that in your code.

Happy coding

I realised I’ve made some mistakes in the print messages themselves and I’ve tried including the error handling methods more. It worked. Thank you!