🧠 Assignment β€’ Flowchart + Framework β€’ Practice with Pyodide

Printing Numbers into Words

Convert a positive integer into words using functions and delegation. Example: 1234 β†’ One Thousand Thirty Four

Ones(n) Tens(n) Hundreds(n) Thousands(n) NumberToWords(n)

1) Problem Statement

Task: Write a program that converts a number into words using separate functions.
Range suggestion (for this assignment): 0 to 9999 (extend later).

2) Given Data (Do Not Modify)

Use these lists. Do not hardcode full answers like "One Thousand Thirty Four".
digits = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"]

decimals = ["Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"]

teens = ["Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]

3) Function Responsibilities (Framework)

Ones(n)
  • Input: 0–9
  • Output: word from digits
  • No extra logic here
Tens(n)
  • Input: 10–99
  • Handle 10–19 using teens
  • Handle 20–99 using decimals + Ones()
Hundreds(n)
  • Input: 100–999
  • Split: hundreds digit + remainder
  • Delegate remainder to Tens()/Ones()
Thousands(n)
  • Input: 1000–9999
  • Split: thousands digit + remainder
  • Delegate remainder to lower functions

NumberToWords(n) (Controller)

This function only decides which helper to call:
  • 0–9 β†’ Ones
  • 10–99 β†’ Tens
  • 100–999 β†’ Hundreds
  • 1000–9999 β†’ Thousands

4) Starter Skeleton (Students Fill This)

# Framework-only skeleton (students must implement the logic)

digits = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"]
decimals = ["Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"]
teens = ["Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]

def Ones(n):
    # TODO
    pass

def Tens(n):
    # TODO
    pass

def Hundreds(n):
    # TODO
    pass

def Thousands(n):
    # TODO
    pass

def NumberToWords(n):
    # TODO
    pass

# Try:
# print(NumberToWords(1234))

5) Pyodide Practice Editor

Write your Python below. Click Run. Output will show in the console.
Loading Pyodide…

6) Reference Solution (Hidden)

Note: This is hidden for self-check. Students should try first.
β–Ά Show Reference Solution
digits = ["Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"]
decimals = ["Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"]
teens = ["Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]

def Ones(n):
    return digits[n]

def Tens(n):
    if n < 10:
        return Ones(n)
    if n < 20:
        return teens[n - 10]
    t = n // 10
    r = n % 10
    return decimals[t - 1] if r == 0 else decimals[t - 1] + " " + Ones(r)

def Hundreds(n):
    h = n // 100
    r = n % 100
    return Ones(h) + " Hundred" if r == 0 else Ones(h) + " Hundred " + Tens(r)

def Thousands(n):
    th = n // 1000
    r = n % 1000
    return Ones(th) + " Thousand" if r == 0 else Ones(th) + " Thousand " + NumberToWords(r)

def NumberToWords(n):
    if n < 10:
        return Ones(n)
    elif n < 100:
        return Tens(n)
    elif n < 1000:
        return Hundreds(n)
    elif n < 10000:
        return Thousands(n)
    else:
        return "Out of range"

# Demo:
print(NumberToWords(1234))
Find Text