Debug an ISBN Validatior

Hey, I am at a complete loss of what to do, as my project is failing the following tests:

6. When the user inputs a value that is not a comma separated value, you should see the message Enter comma-separated values. in the console.
7. When the user inputs a non-numeric value for the length, you should see the message Length must be a number. in the console.
8. When the user enters an incorrect ISBN code with characters other than numbers, you should see the message Invalid character was found. in the console.
9. When the user enters 1530051126,10, you should see the message Valid ISBN Code. in the console.
10. When the user enters 9781530051120,13, you should see the message Valid ISBN Code..
11. When the user enters 1530051125,10, you should see the message Invalid ISBN Code..
12. When the user enters 9781530051120,10, you should see the message ISBN-10 code should be 10 digits long..
13. When the user enters 1530051126,13, you should see the message ISBN-13 code should be 13 digits long..
14. When the user enters 15-0051126,10, you should see the message Invalid character was found..
15. When the user enters 1530051126,9, you should see the message Length should be 10 or 13..
16. When the user enters 1530051125,A, you should see the message Length must be a number..
17. When the user enters 1530051125, you should see the message Enter comma-separated values..
18. When the user enters 9971502100,10, you should see the message Valid ISBN Code..
19. When the user enters 080442957X,10, you should see the message Valid ISBN Code..
20. When the user enters 9781947172104,13, you should see the message Valid ISBN Code..

This is my code:

import re

def validate_isbn(isbn, length):

try:

    length + 1

except TypeError:

    return 'Length must be a number.'

isbn = str(isbn)

if len(isbn) != length:

    print(f'ISBN-{length} code should be {length} digits long.')

    return

main_digits = isbn\[0:length - 1\]

given_check_digit = isbn\[length - 1\]

main_digits_list = \[int(digit) for digit in main_digits\]

\# Calculate the check digit from other 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)

\# Check if the given check digit matches with the calculated check digit

if given_check_digit == expected_check_digit:

    print('Valid ISBN Code.')

else:

    print('Invalid ISBN Code.')

def calculate_check_digit_10(main_digits_list):

\# Note: You don't have to fully understand the logic in this function.

digits_sum = 0

\# Multiply each of the first 9 digits by its corresponding weight (10 to 2) and sum up the results

for index, digit in enumerate(main_digits_list):

    digits_sum += digit \* (10 - index)

\# Find the remainder of dividing the sum by 11, then subtract it from 11

result = 11 - digits_sum % 11

\# The calculation result can range from 1 to 11.

\# If the result is 11, use 0.

\# If the result is 10, use upper case X.

\# Use the value as it is for other numbers.

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):

\# Note: You don't have to fully understand the logic in this function.

digits_sum = 0

\# Multiply each of the first 12 digits by 1 and 3 alternately (starting with 1), and sum up the results

for index, digit in enumerate(main_digits_list):

    if index % 2 == 0:

        digits_sum += digit \* 1

    else:

        digits_sum += digit \* 3

\# Find the remainder of dividing the sum by 10, then subtract it from 10

result = 10 - digits_sum % 10

\# The calculation result can range from 1 to 10.

\# If the result is 10, use 0.

\# Use the value as it is for other numbers.

if result == 10:

    expected_check_digit = '0'

else:

    expected_check_digit = str(result)

return result

isbn = values\[0\]

try:

    length = int(values\[1\])

    if length == 10 or length == 13:

        validate_isbn(isbn, length)

    else:

        print('Length should be 10 or 13.')

except ValueError:

    return 'Length must be a number.'

def main():

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

if ',' not in user_input:

    return 'Enter comma-separated values.'

values = user_input.split(',')

nonNumeric = re.findall(r'^0-9', user_input)

if len(nonNumeric) != 0:

    return 'Invalid character was found.'

isbn = values\[0\]

try:

    length = int(values\[1\])

    if length == 10 or length == 13:

        validate_isbn(isbn, length)

    else:

        print('Length should be 10 or 13.')

except ValueError:

    return 'Length must be a number.'                            #main()

Can you provide a link to the lab?

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Here are some debugging steps you can follow. Focus on one test at a time:

  1. Are there any errors or messages in the console?
  2. What is the requirement of the failing test?
  3. Check the related User Story and ensure it’s followed precisely.
  4. What line of code implements this?
  5. What is the result of the code and does it match the requirement? (Write the value of a variable to the console at that point in the code if needed.)

If this does not help you solve the problem, please reply with answers to these questions.

Hey, I am at a complete loss of what to do, as my project is failing the following tests:

6. When the user inputs a value that is not a comma separated value, you should see the message Enter comma-separated values. in the console.
7. When the user inputs a non-numeric value for the length, you should see the message Length must be a number. in the console.
8. When the user enters an incorrect ISBN code with characters other than numbers, you should see the message Invalid character was found. in the console.
9. When the user enters 1530051126,10, you should see the message Valid ISBN Code. in the console.
10. When the user enters 9781530051120,13, you should see the message Valid ISBN Code..
11. When the user enters 1530051125,10, you should see the message Invalid ISBN Code..
12. When the user enters 9781530051120,10, you should see the message ISBN-10 code should be 10 digits long..
13. When the user enters 1530051126,13, you should see the message ISBN-13 code should be 13 digits long..
14. When the user enters 15-0051126,10, you should see the message Invalid character was found..
15. When the user enters 1530051126,9, you should see the message Length should be 10 or 13..
16. When the user enters 1530051125,A, you should see the message Length must be a number..
17. When the user enters 1530051125, you should see the message Enter comma-separated values..
18. When the user enters 9971502100,10, you should see the message Valid ISBN Code..
19. When the user enters 080442957X,10, you should see the message Valid ISBN Code..
20. When the user enters 9781947172104,13, you should see the message Valid ISBN Code..

This is my code:

import re

def validate_isbn(isbn, length):

import re
\# Note: You don't have to fully understand the logic in this function.

digits_sum = 0

\# Multiply each of the first 9 digits by its corresponding weight (10 to 2) and sum up the results

for index, digit in enumerate(main_digits_list):

    digits_sum += digit \* (10 - index)

\# Find the remainder of dividing the sum by 11, then subtract it from 11

result = 11 - digits_sum % 11

\# The calculation result can range from 1 to 11.

\# If the result is 11, use 0.

\# If the result is 10, use upper case X.

\# Use the value as it is for other numbers.

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):

\# Note: You don't have to fully understand the logic in this function.

digits_sum = 0

\# Multiply each of the first 12 digits by 1 and 3 alternately (starting with 1), and sum up the results

for index, digit in enumerate(main_digits_list):

    if index % 2 == 0:

        digits_sum += digit \* 1

    else:

        digits_sum += digit \* 3

\# Find the remainder of dividing the sum by 10, then subtract it from 10

result = 10 - digits_sum % 10

\# The calculation result can range from 1 to 10.

\# If the result is 10, use 0.

\# Use the value as it is for other numbers.

if result == 10:

    expected_check_digit = '0'

else:

    expected_check_digit = str(result)

return result

isbn = values\[0\]

try:

    length = int(values\[1\])

    if length == 10 or length == 13:

        validate_isbn(isbn, length)

    else:

        print('Length should be 10 or 13.')

except ValueError:

    return 'Length must be a number.'

def main():

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

if ',' not in user_input:

    return 'Enter comma-separated values.'

values = user_input.split(',')

nonNumeric = re.findall(r'^0-9', user_input)

if len(nonNumeric) != 0:

    return 'Invalid character was found.'

isbn = values\[0\]

try:

    length = int(values\[1\])

    if length == 10 or length == 13:

        validate_isbn(isbn, length)

    else:

        print('Length should be 10 or 13.')

except ValueError:

    return 'Length must be a number.'                            #main()

Please post again your code following this guide to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add the backticks.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Tell us what’s happening:

Most tests from 6. onwards are not completed, although I have included the necessary code. No errors are currently returned.

Your code so far

def validate_isbn(isbn, length):
    try:
        length + 1
    except TypeError:
        return 'Length must be a number.'
    isbn = str(isbn)
    if len(isbn) != length:
        print(f'ISBN-{length} code should be {length} digits long.')
        return
    main_digits = isbn[0:length - 1]
    given_check_digit = isbn[length - 1]
    main_digits_list = [int(digit) for digit in main_digits]
    # Calculate the check digit from other 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)
    # Check if the given check digit matches with the calculated check digit
    if given_check_digit == expected_check_digit:
        print('Valid ISBN Code.')
    else:
        print('Invalid ISBN Code.')
def calculate_check_digit_10(main_digits_list):
    # Note: You don't have to fully understand the logic in this function.
    digits_sum = 0
    # Multiply each of the first 9 digits by its corresponding weight (10 to 2) and sum up the results
    for index, digit in enumerate(main_digits_list):
        digits_sum += digit * (10 - index)
    # Find the remainder of dividing the sum by 11, then subtract it from 11
    result = 11 - digits_sum % 11
    # The calculation result can range from 1 to 11.
    # If the result is 11, use 0.
    # If the result is 10, use upper case X.
    # Use the value as it is for other numbers.
    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):
    # Note: You don't have to fully understand the logic in this function.
    digits_sum = 0
    # Multiply each of the first 12 digits by 1 and 3 alternately (starting with 1), and sum up the results
    for index, digit in enumerate(main_digits_list):
        if index % 2 == 0:
            digits_sum += digit * 1
        else:
            digits_sum += digit * 3
    # Find the remainder of dividing the sum by 10, then subtract it from 10
    result = 10 - digits_sum % 10
    # The calculation result can range from 1 to 10.
    # If the result is 10, use 0.
    # Use the value as it is for other numbers.
    if result == 10:
        expected_check_digit = '0'
    else:
        expected_check_digit = str(result)
    return result
    isbn = values[0]
    try:
        length = int(values[1])
        if length == 10 or length == 13:
            validate_isbn(isbn, length)
        else:
            print('Length should be 10 or 13.')
    except ValueError:
        return 'Length must be a number.'

def main():
    user_input = input('Enter ISBN and length: ')
    if ',' not in user_input:
        return 'Enter comma-separated values.'
    values = user_input.split(',')
    nonNumeric = re.findall(r'^0-9', user_input)
    if len(nonNumeric) != 0:
        return 'Invalid character was found.'
    isbn = values[0]
    try:
        length = int(values[1])
        if length == 10 or length == 13:
            validate_isbn(isbn, length)
        else:
            print('Length should be 10 or 13.')
    except ValueError:
        return 'Length must be a number.'

#main()

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/29.0 Chrome/136.0.0.0 Mobile Safari/537.36

Challenge Information:

Debug an ISBN Validator - Debug an ISBN Validator

have you tried answering these questions that pkdvalis shared with you?

Hey, thank you for your help. Here is the link. I was only able to create a new forum from the “ask for help” button, not comment on an existing one. I apologize for the inconvenience.

Yes I have; no errors are printed. I have tried to fulfill all requirements, but more than 3 remain uncompleted, according to the console output. I have added and changed code multiple times regarding those issues, but nothing seems to help.

Have you tried testing your code with invalid or valid inputs mentioned in the tests, and seeing what it returns?

Do you need ton uncomment main() to do this?

When I try to rum such tests, although main() is commented, the console returns “comment main()” and is not returning the results of the tests.

I believe main() is only for if you want input to be given by a user (or yourself in the terminal of course), otherwise the validate_isbn function should do the trick.

Maybe such tests should run in the terminal though, let me check that.

Edit: found one mistake this way, thank you for your help!!!

1 Like