Debug an ISBN Validator - Debug an ISBN Validator

Tell us what’s happening:

This has been a two whole day venture where I’m stuck on the same issue.
I am having trouble with 9, 10, 11 and likely so on; I’m trying to understand what is wrong with 9 right now as my tests come back correct in pythontutor – but I get a value error when i try to run main() in the lab terminal. i assume the problem lies in the main function.
thanks

Your code so far

def validate_isbn(isbn, length):
    if len(str(isbn)) != length:
        return(f'ISBN-{length} code should be {length} digits long.')

    main_digits = isbn[:-1]
    given_check_digit = isbn[-1]
    main_digits_list = [int(digit) for digit in main_digits]
    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:
        return('Valid ISBN Code.')
    else:
        return('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: ')
    values = user_input.split(',')
    if ',' not in user_input:
        print('Enter comma-separated values.')
        return
    isbn = values[0]
    try:
        isbn = int(isbn)
    except ValueError:
        print('Invalid character was found.')
    try:
        length = int(values[1])
    except ValueError:
        print('Length must be a number.')
    if length == 10 or length == 13:
        return validate_isbn(str(isbn), length)
    else:
        return('Length should be 10 or 13.')


(main())


#print(validate_isbn('1530051126',10))
#print(validate_isbn('1530051125',10))
#print(validate_isbn('9781530051120',10))
#print(validate_isbn(15-0051126,10))
#print(validate_isbn('1530051126',9))
#print(validate_isbn(153005115,A))
#print(validate_isbn(1530051125 5))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/149.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

Hi @jjackintheboxx

First you need to comment out some code:

You should comment out the call to the main function to allow for the rest of the tests to work properly.

To help you debug, print out the main_digit variable.

Happy coding

why don’t you try to run with main() uncomented in pythontutor? it will also allows you to put in the input requested by input()

you are also not following the “you should see the message in the console” part

hello!

Don’t worry, I have been commenting out main()! using it was just a desperate test to understand what i had been doing wrong
I have printed the main_digitsvariable, but it gives me what I expected, the isbn number without the last digit. should it be something else?

I have done this! but thats when it passes through everything.

and can you clarify the last part for me? because I do get them in the terminal.

Hi @jjackintheboxx

image

Can you please the difference between the two print calls?

Happy coding

this line returns this, which line prints it?

same question for these others