Error in the lab section of python certification

I

finished the code and it is showing the exact results as asked with the correct punctuation but it still showing error for the particular task

Your code so far

checkpoint=None
def validate_isbn(isbn, length):
    global checkpoint
    if len(isbn) != length:
        print(f'ISBN-{length} code should be {length} digits long.')
        raise ValueError('The ISBN code should only have numbers.')
        return
    main_digits = isbn[0:length-1]
    given_check_digit = isbn[length-1]
    checkpoint='isbn'
    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 expected_check_digit
def main():
    global checkpoint
    user_input = input('Enter ISBN and length: ')
    values = user_input.split(',')
    if len(values) !=2:
        print('Enter comma-separated values.')
        return
    print (values)
    isbn = values[0]
    checkpoint='length'
    length = int(values[1])
    if length == 10 or length == 13:
        validate_isbn(isbn, length)
    else:
        print('Length should be 10 or 13.')
try:
    main()
except IndexError:
    print ('The input should have isbn value and its length seprated by a comma')
except ValueError:
    if checkpoint == 'length':
        print('Length must be a number.')
    if checkpoint == 'isbn':
        print('Invalid character was found.')


Lesson URL (copy - paste from your browser’s address bar)

Welcome to the forum @theva

Please post a link to the lab.

Happy coding

Debug an ISBN Validator: Debug an ISBN Validator | freeCodeCamp.org

Welcome to the forum @theva,

I recommend using try/except inside main rather than in the global space.

Happy coding

Hi @theva

Important: you will need to comment out the main() call in the global space for the tests to run properly.

You are not asked to call the main function for this lab.

To help you debug, add the following print call at the end of the editor:

print(validate_isbn(153005112, 10))

Happy coding

Hi Dhess,

I tried putting the try/except inside the main function and worked. thank you so much :slight_smile:

yes, it worked now. Thank you so much Teller :slight_smile:

Hi,

sorry but I have problem with another lab section as well. can you please help me with some guidance ?

here is my code:

class Category:

def \__init_\_(self,name):

    self.name = name

    self.ledger = \[\]



def get_balance(self):

    balance = 0

    for item in self.ledger:

        balance += item\['amount'\]



    return balance



def check_funds(self,amount):

    if amount > self.get_balance():

        return False

    else:

        return True



def deposit(self,amount,description=''):

    self.ledger.append({'amount': amount, 'description': description})



def withdraw(self,amount,description=''):

    if self.check_funds(amount):

        self.ledger.append({'amount': -1\*amount, 'description': description})

        return True

    else:

        return False




def transfer(self,amount,other):

    if self.check_funds(amount):

        self.withdraw(amount,f'Transfer to {other.name}')

        other.deposit(amount,f'Transfer from {self.name}')

        return True



    else:

        return False





def \__str_\_(self):

    l = len(self.name)

    prefix\_=((30-l)//2)\*'\*'

    suffix\_=(30-l-len(prefix\_))\*'\*'

    line ='\\n'+prefix\_+self.name+suffix\_

    for transaction in self.ledger:

        t = transaction

        amount =format(t\['amount'\],".2f")

        des = t\['description'\]

        if len(des) <= 23:

            if len(str(amount)) <=7:

                line += f"\\n{des}{(23-len(des))\*' '}{(7-len(str(amount)))\*' '}{str(amount)}"

            else:

                line += f"\\n{des}{(23-len(des))\*' '}{str(amount)\[0:7\]}"

        

        else:

            if len(str(amount)) <=7:

                line += f"\\n{des\[0:23\]}{(7-len(str(amount)))\*' '}{str(amount)}"

            else:

                line += f"\\n{des\[0:23\]}{str(amount)\[0:7\]}"



        

        

    line += f"\\nTotal: {self.get_balance()}\\n"



    return line

def create_spend_chart(categories):

line = 'Percentage spent by category'



spent_amounts = \[\]

for category in categories:

    spent_amount = 0

    for transaction in category.ledger:

        t = transaction

        if t\['amount'\] < 0:

            spent_amount += abs(t\['amount'\])

    

    spent_amounts.append({'category': category.name,'amount':spent_amount})

    

total = sum(spend\['amount'\] for spend in spent_amounts)



percentages = \[\]

for spend in spend_amounts:

    per = spend\['amount'\]\*100/total

    percentages.append({'category':spend\['category'\],'%\_spent':int((per//10)\*10)})



for y in range (100,-1,-10):

    line += f'\\n{y}| '

    for item in percentages:

        if item\['%\_spent'\] >= y:

            line += 'o  '

        else:

            line += '   '

    



line += '\\n    '+ len(categories)\*'---'



n = 1

while True:

    n += 1

    space_count = 0

    line += '\\n     '

    for item in percentages:

        if len(item\['category'\]) >= n:

            if n == 1:

                line += item\['category'\]\[n-1\].upper() + '  '

            else:

                line += item\['category'\]\[n-1\] + '  '

        else:

            line += '   '

            space_count += 1



    if space_count >= len(percentages):

        break



return line

and here is the link to the lab:

Build a Budget App: Build a Budget App | freeCodeCamp.org

Hi @theva,

Please create a new topic for this issue.

Thank you.