Digital root
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
This one builds on a lesson you haven't finished yet. Do that lesson first:
Python Basics · Functions →
Digital root
Keep adding digits until a single digit remains.
Two loops work together here. The inner for adds up the digits once; str(n) lets you loop over them as characters. The outer while repeats that as long as the number still has two or more digits, which is while n >= 10.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
Write digital_root(n) that adds up the digits of n, then the digits of that answer, and so on until one digit is left. 9875 gives 29, then 11, then 2.
Click Run to see the output here.