Home My space log
Language EN (English)

Barcode: the last digit

Python Intermediate alien ages 11-13 From CS Unplugged

The problem

A 13-digit barcode ends in a check digit. Write a function barcode_check_digit(first12) that gets the first 12 digits as text and gives back the last digit as a number. How: add the digits, but multiply every second one (the 2nd, 4th, 6th ...) by 3. The check digit is (10 - total % 10) % 10.

You will practice: check digits, loops, remainder (%)

Mode

Press Tab to indent. Press Esc and then Tab to move on to the next button.

Your code runs in your browser. We do not receive it or save it. Python is downloaded from cdn.jsdelivr.net the first time you run it (about 10 MB).

Test results

      What your code printed

      
          

      CC BY-SA 4.0

      Adapted from a CS Unplugged programming challenge by the University of Canterbury Computer Science Education Research Group, licensed CC BY-SA 4.0.

      Original challenge · License: CC BY-SA 4.0

      Changes: Rewritten in our own words as a function with tests, hints and steps, in Python and JavaScript, by Open Cosmos. This version is also CC BY-SA 4.0.