Back to Freecodecamp

Step 26

curriculum/challenges/english/blocks/learn-how-to-work-with-numbers-and-strings-by-implementing-the-luhn-algorithm/65687c8d86e18cbd775a53c9.md

latest1.8 KB
Original Source

--description--

The next part of the Luhn Algorithm is to multiply all the even digits by 2.

Within the even digit for loop, replace the print call with a variable named number and assign it the value of digit multiplied by 2.

--hints--

You should have number = int(digit) * 2 within the for loop.

js
({
    test: () => {
        const transformedCode = code.replace(/\r/g, "");
        const verify_card_number = __helpers.python.getDef("\n" + transformedCode, "verify_card_number");
        const { function_body } = verify_card_number;

        assert.match(function_body, /number\s*=\s*int\(\s*digit\s*\)\s*\*\s*2/);
    }
})

You should not have print(digit) within the for loop.

js
({
    test: () => {
        const transformedCode = code.replace(/\r/g, "");
        const verify_card_number = __helpers.python.getDef("\n" + transformedCode, "verify_card_number");
        const { function_body } = verify_card_number;

        const no_comments = __helpers.python.removeComments(function_body);

        assert.notMatch(no_comments, /print\s*\(\s*digit\s*\)/);
    }
})

--seed--

--seed-contents--

py
def verify_card_number(card_number):
    sum_of_odd_digits = 0
    card_number_reversed = card_number[::-1]
    odd_digits = card_number_reversed[::2]

    for digit in odd_digits:
        sum_of_odd_digits += int(digit)

--fcc-editable-region--
    sum_of_even_digits = 0
    even_digits = card_number_reversed[1::2]
    for digit in even_digits:
        print(digit)
--fcc-editable-region--
def main():
    card_number = '4111-1111-4555-1142'
    card_translation = str.maketrans({'-': '', ' ': ''})
    translated_card_number = card_number.translate(card_translation)

    verify_card_number(translated_card_number)

main()