-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
2137 lines (1959 loc) · 103 KB
/
Copy pathapp.py
File metadata and controls
2137 lines (1959 loc) · 103 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Code Quest backend — Flask + SQLite (Turso), multi-family.
Single process serves both the frontend (static/index.html) and the JSON
API. Parents authenticate via Google OAuth; kids authenticate via a
per-kid generated handle + short PIN. Every request that touches
kid/task/submission/redemption/settings data resolves `family_id` from
the session (never from a client-supplied value) and every route that
reads or writes a specific record verifies that record actually belongs
to that family before acting.
Run with: python app.py
Then visit http://<this-machine's-LAN-IP>:5000 from any device on the
same network (or the deployed Render URL).
"""
import hashlib
import json
import os
import random
import secrets
import time
import uuid
from datetime import date, datetime, timedelta
from functools import wraps
from authlib.integrations.flask_client import OAuth
from flask import Flask, g, jsonify, redirect, request, send_from_directory, session, url_for
import db as dbmod
app = Flask(__name__, static_folder="static", static_url_path="")
ON_RENDER = os.environ.get("RENDER") == "true"
app.config["SECRET_KEY"] = os.environ.get("FLASK_SECRET_KEY") or (
# Fail loudly in production rather than silently running with a
# guessable key; fine to fall back for quick local testing only.
(_ for _ in ()).throw(RuntimeError("FLASK_SECRET_KEY must be set on Render"))
if ON_RENDER else "dev-only-insecure-key-do-not-use-in-production"
)
app.config["SESSION_COOKIE_HTTPONLY"] = True
app.config["SESSION_COOKIE_SAMESITE"] = "Lax"
app.config["SESSION_COOKIE_SECURE"] = ON_RENDER
app.config["PERMANENT_SESSION_LIFETIME"] = timedelta(days=30)
oauth = OAuth(app)
oauth.register(
name="google",
client_id=os.environ.get("GOOGLE_OAUTH_CLIENT_ID"),
client_secret=os.environ.get("GOOGLE_OAUTH_CLIENT_SECRET"),
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "openid email profile"},
)
DEFAULT_SETTINGS = {"pointsPerMinute": "1", "dailyCapMinutes": "60"}
# Canonical starter content cloned (with freshly generated ids) into any
# newly created kid's own task list — see seed_starter_tasks_for_kid().
# Never reused as literal ids: two different kids each get their own
# independent copy of these rows.
# goto/setheading, pensize and speed had no tasks anywhere in the corpus,
# so the skills existed with nothing to practise on. These fill that in.
POSITIONING_TASKS = [
("Four Corners", 10, "easy",
"Put a dot in each corner of the screen using goto(x, y) — try (-150, 150), (150, 150), (150, -150) and (-150, -150). Use penup() so you don't draw lines between them.",
["positioning", "pen_control"]),
("Scattered Stars", 20, "medium",
"Draw five small stars at five different spots, using penup() and goto() to move between them so no lines join them up.",
["positioning", "pen_control", "loops_basic"]),
("Point the Way", 10, "easy",
"Use setheading() to point the turtle in four different directions — 0 is right, 90 is up, 180 is left, 270 is down — drawing a short line each time.",
["positioning"]),
("Thick and Thin", 10, "easy",
"Draw three lines side by side with pensize() set to 1, then 5, then 15, so you can see the pen getting thicker.",
["pen_control"]),
("Dotted Trail", 10, "easy",
"Use dot() inside a loop to leave a trail of dots across the screen instead of a solid line. Change the dot size as you go.",
["pen_control", "loops_basic"]),
("Back to the Middle", 10, "easy",
"Draw a shape anywhere on screen, then send the turtle back to the centre with goto(0, 0) without leaving a line behind it.",
["positioning", "pen_control"]),
]
STARTER_TURTLE_TASKS = POSITIONING_TASKS + [
("Draw Your Initials", 10, "easy",
"Draw your initials using only forward, right, left, penup, and pendown. No loops required — but you might find you want one.",
["shapes"]),
("Any-Sided Shape", 10, "easy",
"Write code that can draw a triangle, pentagon, hexagon — any shape — by changing just one number. Figure out the relationship between number of sides and turn angle.",
["shapes", "loops_basic"]),
("Draw a House", 10, "easy",
"Combine a square and a triangle to draw a simple house shape — walls plus a roof, in one continuous drawing.",
["shapes"]),
("Rainbow Line", 10, "easy",
"Draw a series of lines side by side, each a different color, using a loop and t.color().",
["colors", "loops_basic"]),
("Smiley Face", 10, "easy",
"Use t.circle() plus penup/pendown to draw a face — one big circle, two small circles for eyes, a curved mouth.",
["shapes"]),
("Nested Squares", 10, "easy",
"Draw several squares of increasing size, one inside the other, using a single loop where the side length grows each time.",
["shapes", "loops_basic"]),
("Flower with Circles", 10, "easy",
"Draw several overlapping circles arranged in a ring, using a loop that turns the turtle a bit before drawing each circle.",
["shapes", "loops_basic"]),
("Draw Your Number", 10, "easy",
"Pick a number that means something to you (your age, your hockey jersey number) and draw it big using lines and curves.",
["shapes"]),
("Dotted Path", 10, "easy",
"Use t.dot() inside a loop to draw a dashed or dotted line or shape instead of a solid one.",
["loops_basic"]),
("Maze Border", 10, "easy",
"Draw a rectangle border big enough to be a maze outline, and mark the starting corner with a dot.",
["shapes"]),
("Growing Spiral", 20, "medium",
"Draw a spiral where each side is longer than the last. You'll need a loop where the forward distance changes each time through.",
["loops_basic"]),
("Checkerboard Grid", 20, "medium",
"Draw an 8x8 grid of squares, alternating filled and unfilled. This needs a loop inside a loop.",
["nested_loops"]),
("Random Walk", 20, "medium",
"Make the turtle take 50 random steps in random directions using the random module. Bonus: change color as it goes.",
["randomness", "loops_basic"]),
("Five-Pointed Star", 20, "medium",
"Draw a five-pointed star without lifting the pen — one loop, one clever turn angle. Hunt for the angle yourself before looking it up.",
["loops_basic"]),
("Traffic Light Simulator", 30, "hard",
"Draw three circles (red, yellow, green). Using time.sleep(), light up one at a time in sequence by filling it in.",
["functions"]),
("Recursive Tree", 30, "hard",
"Draw a branching tree using a function that calls itself. Ask an AI to explain recursion conceptually first, then build it yourself.",
["recursion", "functions"]),
("Keyboard Etch-a-Sketch", 30, "hard",
"Arrow keys move the turtle, spacebar changes color, a key clears the screen. Real interactivity, not just run-once.",
["event_handling"]),
("Design Your Own", 30, "hard",
"Pick something you want the turtle to draw or do. Break it into steps yourself before writing any code.",
[]),
("Zigzag Path", 10, "easy",
"Draw a zigzag line by alternating turning right and left inside a loop, moving forward a bit each time.",
["loops_basic"]),
("Polka Dot Row", 10, "easy",
"Use t.dot() inside a loop to draw a neat row of evenly-spaced dots, moving forward between each one.",
["loops_basic"]),
("Simple Arrow", 10, "easy",
"Draw an arrow — a straight shaft with a small triangular head — using only forward, right, and left.",
["shapes"]),
("Color Blocks", 10, "easy",
"Draw four filled squares in a row, each a different color, using begin_fill() and end_fill().",
["colors", "shapes"]),
("Star Necklace", 10, "easy",
"Draw a row of small five-pointed stars strung along an invisible line — pen up to move between each one.",
["shapes", "loops_basic"]),
("Picture Frame", 10, "easy",
"Draw a rectangle, then a smaller rectangle inside it, so it looks like a picture frame.",
["shapes"]),
("Sunburst Lines", 10, "easy",
"Draw 12 lines radiating out from the center point like sun rays, using a loop that turns the same angle each time.",
["loops_basic"]),
("Traffic Cone", 10, "easy",
"Draw a triangle on top of a small rectangle base, colored orange, to look like a traffic cone.",
["shapes", "colors"]),
("Your Initials, Twice", 10, "easy",
"Draw your first and last initial side by side, reusing the same drawing code for both if you can.",
["shapes"]),
("Bullseye Target", 10, "easy",
"Draw four circles of the same center point but different sizes, alternating between two colors, like an archery target.",
["shapes", "colors", "loops_basic"]),
("Color-Alternating Fence", 20, "medium",
"Draw a row of fence posts, alternating between two colors using an if/else inside your loop based on whether the post number is even or odd.",
["colors", "loops_basic", "conditionals"]),
("Grid of Dots", 20, "medium",
"Draw an NxN grid of dots using a loop inside a loop — the outer loop for rows, the inner loop for columns.",
["nested_loops"]),
("Random Confetti", 20, "medium",
"Scatter 100 small dots of random colors at random positions on the screen using the random module.",
["randomness"]),
("Reusable Flower Function", 20, "medium",
"Write a function draw_flower(size) that draws one flower, then call it three times at different spots to plant a small garden.",
["functions", "shapes"]),
("Rainbow Nested Squares", 20, "medium",
"Draw several squares nested inside each other like Nested Squares, but give each one the next color from a list as you go.",
["nested_loops", "colors"]),
("Recursive Snowflake", 30, "hard",
"Write a function that draws a jagged snowflake edge by calling itself with a smaller size each time, stopping at a small base-case size.",
["recursion", "functions"]),
("Turtle Race Game", 30, "hard",
"Two turtles start at the same line. Each turn, move each one forward a random amount; stop and print the winner once one crosses a finish line.",
["randomness", "conditionals", "loops_basic"]),
("Click-to-Draw", 30, "hard",
"Use turtle's onscreenclick so that clicking anywhere on the canvas draws a dot there — build a picture just by clicking around.",
["event_handling"]),
("Guarded Drawing", 30, "hard",
"Write a function that only draws a shape if a variable you set in your code matches a specific value — otherwise it does nothing.",
["functions", "conditionals"]),
("Star Generator Function", 30, "hard",
"Write a function star(n) that can draw a star with ANY number of points by figuring out the correct turn angle inside the function itself.",
["functions", "loops_basic"]),
]
STARTER_JUDGE_TASKS = [
("Even or Odd", 10, "easy",
"Read a whole number from input() and print \"even\" if it's even, or \"odd\" if it's odd.",
["conditionals"],
[{"input": ["4"], "expected": "even"}, {"input": ["7"], "expected": "odd"}, {"input": ["0"], "expected": "even"}]),
("FizzBuzz", 20, "medium",
"Read a whole number n from input(). For each number from 1 to n, print the number -- but print \"Fizz\" instead if it's divisible by 3, \"Buzz\" if it's divisible by 5, and \"FizzBuzz\" if it's divisible by both.",
["loops_basic", "conditionals"],
[{"input": ["15"], "expected": "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz"}]),
("Staircase Ways", 35, "hard",
"Read a whole number n -- the number of stairs. You can climb 1 or 2 stairs at a time. Print how many different ways there are to reach the top. (A plain recursive solution works for small n, but gets very slow for bigger ones -- memoization is what makes it fast for any input.)",
["recursion", "dynamic_programming"],
[{"input": ["1"], "expected": "1"}, {"input": ["5"], "expected": "8"}, {"input": ["10"], "expected": "89"}, {"input": ["15"], "expected": "987"}]),
]
STARTER_FUNDAMENTALS_TASKS = [
("Say Hello", 3, "fundamentals",
"Read your name from input(), store it in a variable, and print \"Hello, \" followed by your name.",
["variables"],
[{"input": ["Shayne"], "expected": "Hello, Shayne"}, {"input": ["Kelly"], "expected": "Hello, Kelly"}, {"input": ["Ada"], "expected": "Hello, Ada"}]),
("Text or Number?", 3, "fundamentals",
"Read a number from input() and print it doubled. Remember: input() always gives you text -- wrap it in int() to treat it as a real number.",
["type_casting"],
[{"input": ["5"], "expected": "10"}, {"input": ["0"], "expected": "0"}, {"input": ["-4"], "expected": "-8"}]),
("Count to Ten", 3, "fundamentals",
"Print the numbers 1 through 10, one per line.",
["loops_basic"],
[{"input": [], "expected": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"}]),
("Sum Three Numbers", 3, "fundamentals",
"Read 3 numbers from input() (one per line), store them in a list, and print their sum.",
["lists"],
[{"input": ["1", "2", "3"], "expected": "6"}, {"input": ["10", "20", "5"], "expected": "35"}]),
("Shout Three Times", 3, "fundamentals",
"Write a function called shout that prints \"HELLO!\". Call your function 3 times.",
["functions"],
[{"input": [], "expected": "HELLO!\nHELLO!\nHELLO!"}]),
("Double It", 3, "fundamentals",
"Write a function called double that takes one parameter and returns it multiplied by 2. Read a number from input(), call double() on it, and print the result.",
["parameters"],
[{"input": ["4"], "expected": "8"}, {"input": ["10"], "expected": "20"}, {"input": ["0"], "expected": "0"}]),
("Store and Reuse", 3, "fundamentals",
"Read a word from input(), store it in a variable, then print that word three times, once per line -- using the variable each time (not typing the word again).",
["variables"],
[{"input": ["cat"], "expected": "cat\ncat\ncat"}, {"input": ["Shayne"], "expected": "Shayne\nShayne\nShayne"}]),
("Countdown", 3, "fundamentals",
"Read a whole number n from input() and print the numbers from n down to 1, one per line, using a loop.",
["loops_basic"],
[{"input": ["5"], "expected": "5\n4\n3\n2\n1"}, {"input": ["3"], "expected": "3\n2\n1"}]),
("Sum With a Loop", 3, "fundamentals",
"Read a whole number n from input(). Using a loop (not the sum() function), add up all the numbers from 1 to n and print the total.",
["loops_basic"],
[{"input": ["5"], "expected": "15"}, {"input": ["10"], "expected": "55"}, {"input": ["1"], "expected": "1"}]),
("Biggest of Three", 3, "fundamentals",
"Read 3 numbers from input() (one per line), store them in a list, and print the largest one using max().",
["lists"],
[{"input": ["3", "9", "5"], "expected": "9"}, {"input": ["10", "2", "7"], "expected": "10"}]),
("Function Reuse", 3, "fundamentals",
"Write a function called line that prints \"-----\". Call your function 5 times to print 5 lines.",
["functions"],
[{"input": [], "expected": "-----\n-----\n-----\n-----\n-----"}]),
("Add Two Numbers", 3, "fundamentals",
"Write a function called add_two that takes two parameters and returns their sum. Read two numbers from input() (one per line), call add_two() on them, and print the result.",
["parameters"],
[{"input": ["3", "4"], "expected": "7"}, {"input": ["10", "-2"], "expected": "8"}, {"input": ["0", "0"], "expected": "0"}]),
("Remainder Check", 3, "fundamentals",
"Read a whole number from input() and print the remainder when it's divided by 3, using the % operator.",
["simple_math"],
[{"input": ["10"], "expected": "1"}, {"input": ["9"], "expected": "0"}, {"input": ["14"], "expected": "2"}]),
("Average of Two", 3, "fundamentals",
"Read two numbers from input() (one per line) and print their average.",
["simple_math"],
[{"input": ["4", "6"], "expected": "5.0"}, {"input": ["10", "5"], "expected": "7.5"}, {"input": ["0", "0"], "expected": "0.0"}]),
("Seeded Random Number", 3, "fundamentals",
"Import random. Read a whole number from input() and use it as a seed with random.seed(). Then print the result of random.randint(1, 10). Using the same seed always produces the same result -- that's what makes this testable!",
["random_library"],
[{"input": ["1"], "expected": "5"}, {"input": ["7"], "expected": "1"}, {"input": ["42"], "expected": "4"}]),
("Seeded Random Choice", 3, "fundamentals",
"Import random. Read a whole number from input() and use it as a seed with random.seed(). Create a list of words: [\"apple\", \"banana\", \"cherry\", \"date\"]. Print the result of random.choice() on that list.",
["random_library"],
[{"input": ["2"], "expected": "banana"}, {"input": ["5"], "expected": "apple"}, {"input": ["8"], "expected": "date"}]),
("Favorite Color", 3, "fundamentals",
"Read a color from input(), store it in a variable, and print \"My favorite color is \" followed by it.",
["variables"],
[{"input": ["red"], "expected": "My favorite color is red"}, {"input": ["blue"], "expected": "My favorite color is blue"}]),
("Store Your Age", 3, "fundamentals",
"Read your age from input(), store it in a variable, and print what your age will be next year.",
["variables"],
[{"input": ["10"], "expected": "Next year I will be 11"}, {"input": ["5"], "expected": "Next year I will be 6"}]),
("Swap Two Words", 3, "fundamentals",
"Read two words from input() (one per line) and print them swapped, second word first, separated by a space.",
["variables"],
[{"input": ["cat", "dog"], "expected": "dog cat"}, {"input": ["up", "down"], "expected": "down up"}]),
("Full Name", 3, "fundamentals",
"Read a first name, then a last name, and print \"Hello, \" followed by the full name.",
["variables"],
[{"input": ["Ada", "Lovelace"], "expected": "Hello, Ada Lovelace"}]),
("Temperature Label", 3, "fundamentals",
"Read a number from input(), store it in a variable, and print it followed by \" degrees\".",
["variables"],
[{"input": ["72"], "expected": "72 degrees"}, {"input": ["100"], "expected": "100 degrees"}]),
("Store Then Change", 3, "fundamentals",
"Read a number, store it in a variable, add 10 to that same variable, then print it.",
["variables"],
[{"input": ["5"], "expected": "15"}, {"input": ["0"], "expected": "10"}, {"input": ["-3"], "expected": "7"}]),
("Reassign It", 3, "fundamentals",
"Read a word into a variable, then reassign that same variable to the word \"banana\", and print it.",
["variables"],
[{"input": ["apple"], "expected": "banana"}, {"input": ["anything"], "expected": "banana"}]),
("Count the Letters", 3, "fundamentals",
"Read a word from input(), store its length in a variable using len(), and print it.",
["variables"],
[{"input": ["cat"], "expected": "3"}, {"input": ["hello"], "expected": "5"}]),
("Nickname Tag", 3, "fundamentals",
"Read a name into a variable and print \"Hi \" followed by the name followed by \"!!!\".",
["variables"],
[{"input": ["Sam"], "expected": "Hi Sam!!!"}]),
("Total in a Variable", 3, "fundamentals",
"Read two numbers, add them into a variable called total, and print total.",
["variables"],
[{"input": ["3", "4"], "expected": "7"}, {"input": ["10", "-2"], "expected": "8"}]),
("Price Tag", 3, "fundamentals",
"Read a price into a variable and print a dollar sign followed by it.",
["variables"],
[{"input": ["5"], "expected": "$5"}, {"input": ["19"], "expected": "$19"}]),
("Add As Numbers", 3, "fundamentals",
"Read two numbers from input() and print their sum -- remember input() gives you text, so cast with int() first.",
["type_casting"],
[{"input": ["2", "3"], "expected": "5"}, {"input": ["10", "20"], "expected": "30"}]),
("Float It", 3, "fundamentals",
"Read a number from input(), cast it with float(), and print it doubled.",
["type_casting"],
[{"input": ["3"], "expected": "6.0"}, {"input": ["2.5"], "expected": "5.0"}]),
("String of a Number", 3, "fundamentals",
"Read a number, cast it to int(), then use str() to combine it with a text label and print it.",
["type_casting"],
[{"input": ["7"], "expected": "Value: 7"}, {"input": ["42"], "expected": "Value: 42"}]),
("Whole Number Only", 3, "fundamentals",
"Read a decimal number from input() and print just the whole-number part (cast to float, then to int).",
["type_casting"],
[{"input": ["3.9"], "expected": "3"}, {"input": ["7.1"], "expected": "7"}]),
("Compare Two Numbers", 3, "fundamentals",
"Read two numbers as int() and print \"bigger\" if the first is bigger than the second, otherwise \"smaller or equal\".",
["type_casting"],
[{"input": ["5", "3"], "expected": "bigger"}, {"input": ["2", "8"], "expected": "smaller or equal"}]),
("Triple It (Casting)", 3, "fundamentals",
"Read a number, cast it to int(), and print it multiplied by 3.",
["type_casting"],
[{"input": ["4"], "expected": "12"}, {"input": ["0"], "expected": "0"}]),
("You Entered It", 3, "fundamentals",
"Read a number, cast it to int(), then use str() to print \"You entered \" followed by the number.",
["type_casting"],
[{"input": ["9"], "expected": "You entered 9"}, {"input": ["100"], "expected": "You entered 100"}]),
("Divide Evenly", 3, "fundamentals",
"Read two whole numbers and print the result of dividing them with // (integer division, no decimal).",
["type_casting"],
[{"input": ["10", "3"], "expected": "3"}, {"input": ["9", "3"], "expected": "3"}, {"input": ["7", "2"], "expected": "3"}]),
("Percent as Number", 3, "fundamentals",
"Read a whole number representing a percent and print it divided by 100.",
["type_casting"],
[{"input": ["50"], "expected": "0.5"}, {"input": ["25"], "expected": "0.25"}]),
("Age in Months", 3, "fundamentals",
"Read an age in years (may have a decimal) as float() and print it multiplied by 12.",
["type_casting"],
[{"input": ["2"], "expected": "24.0"}, {"input": ["0.5"], "expected": "6.0"}]),
("Convert and Compare to Zero", 3, "fundamentals",
"Read a number as int() and print \"positive\" if it's greater than 0, otherwise \"not positive\".",
["type_casting"],
[{"input": ["5"], "expected": "positive"}, {"input": ["-3"], "expected": "not positive"}, {"input": ["0"], "expected": "not positive"}]),
("Even Numbers Up To N", 3, "fundamentals",
"Read a whole number n and print every even number from 2 up to n, one per line.",
["loops_basic"],
[{"input": ["10"], "expected": "2\n4\n6\n8\n10"}, {"input": ["6"], "expected": "2\n4\n6"}]),
("Multiplication Table", 3, "fundamentals",
"Read a number n and print n times 1 through n times 5, one per line.",
["loops_basic"],
[{"input": ["3"], "expected": "3\n6\n9\n12\n15"}, {"input": ["2"], "expected": "2\n4\n6\n8\n10"}]),
("Repeat a Word", 3, "fundamentals",
"Read a word, then read a count, and print the word that many times, one per line.",
["loops_basic"],
[{"input": ["hi", "3"], "expected": "hi\nhi\nhi"}, {"input": ["go", "1"], "expected": "go"}]),
("Sum of Squares", 3, "fundamentals",
"Read a whole number n and print the sum of the squares of 1 through n, using a loop.",
["loops_basic"],
[{"input": ["3"], "expected": "14"}, {"input": ["1"], "expected": "1"}, {"input": ["4"], "expected": "30"}]),
("Count Down By Twos", 3, "fundamentals",
"Read an even number n and count down to 0 by twos, one number per line.",
["loops_basic"],
[{"input": ["6"], "expected": "6\n4\n2\n0"}, {"input": ["4"], "expected": "4\n2\n0"}]),
("Stars in a Row", 3, "fundamentals",
"Read a whole number n and print a single line made of n asterisks, built up inside a loop.",
["loops_basic"],
[{"input": ["5"], "expected": "*****"}, {"input": ["3"], "expected": "***"}]),
("While Countdown", 3, "fundamentals",
"Read a whole number n and use a while loop to count down from n to 1, one per line.",
["loops_basic"],
[{"input": ["3"], "expected": "3\n2\n1"}, {"input": ["1"], "expected": "1"}]),
("Largest So Far", 3, "fundamentals",
"Read 5 numbers, one per line, and print the largest one -- track it in a variable as you go, without using max().",
["loops_basic"],
[{"input": ["3", "9", "1", "4", "2"], "expected": "9"}, {"input": ["5", "5", "5", "5", "5"], "expected": "5"}]),
("Skip By Threes", 3, "fundamentals",
"Read a whole number n and print every multiple of 3 from 3 up to n, one per line.",
["loops_basic"],
[{"input": ["9"], "expected": "3\n6\n9"}, {"input": ["10"], "expected": "3\n6\n9"}]),
("Count Matches", 3, "fundamentals",
"Read a whole number n, then read n words one per line, and print how many of them are exactly \"yes\".",
["loops_basic"],
[{"input": ["3", "yes", "no", "yes"], "expected": "2"}, {"input": ["2", "no", "no"], "expected": "0"}]),
("Running Total Printed Each Step", 3, "fundamentals",
"Read a whole number n and print the running total after adding each number from 1 to n.",
["loops_basic"],
[{"input": ["3"], "expected": "1\n3\n6"}, {"input": ["4"], "expected": "1\n3\n6\n10"}]),
("Smallest of Three", 3, "fundamentals",
"Read 3 numbers, store them in a list, and print the smallest one using min().",
["lists"],
[{"input": ["3", "9", "5"], "expected": "3"}, {"input": ["10", "2", "7"], "expected": "2"}]),
("Average of a List", 3, "fundamentals",
"Read 3 numbers into a list and print their average using sum() and len().",
["lists"],
[{"input": ["3", "6", "9"], "expected": "6.0"}, {"input": ["10", "20", "30"], "expected": "20.0"}]),
("Second Item", 3, "fundamentals",
"Read 3 words into a list and print the second one (index 1).",
["lists"],
[{"input": ["cat", "dog", "fish"], "expected": "dog"}]),
("Last Item", 3, "fundamentals",
"Read 3 numbers into a list and print the last one using index -1.",
["lists"],
[{"input": ["1", "2", "3"], "expected": "3"}, {"input": ["7", "8", "9"], "expected": "9"}]),
("Build a List With a Loop", 3, "fundamentals",
"Read a whole number n, then read n numbers one per line, appending each to a list, and print the list's length.",
["lists"],
[{"input": ["3", "1", "2", "3"], "expected": "3"}, {"input": ["2", "5", "6"], "expected": "2"}]),
("Sum a Growing List", 3, "fundamentals",
"Read a whole number n, then read n numbers appending each to a list with a loop, and print their sum.",
["lists"],
[{"input": ["3", "1", "2", "3"], "expected": "6"}, {"input": ["2", "10", "20"], "expected": "30"}]),
("Count in a List", 3, "fundamentals",
"Given the list [1, 2, 2, 3, 2], read a target number and print how many times it appears in the list.",
["lists"],
[{"input": ["2"], "expected": "3"}, {"input": ["1"], "expected": "1"}, {"input": ["5"], "expected": "0"}]),
("List of Squares", 3, "fundamentals",
"Read a whole number n, build a list of the squares of 1 through n, then print each one on its own line.",
["lists"],
[{"input": ["3"], "expected": "1\n4\n9"}, {"input": ["4"], "expected": "1\n4\n9\n16"}]),
("First and Last", 3, "fundamentals",
"Read 3 numbers into a list and print the first one plus the last one.",
["lists"],
[{"input": ["1", "2", "3"], "expected": "4"}, {"input": ["10", "5", "1"], "expected": "11"}]),
("Reverse Print", 3, "fundamentals",
"Read 3 words into a list and print them in reverse order, one per line.",
["lists"],
[{"input": ["one", "two", "three"], "expected": "three\ntwo\none"}]),
("Is It In The List", 3, "fundamentals",
"Given a fixed list of fruits, read a word and print \"yes\" if it's in the list, otherwise \"no\".",
["lists"],
[{"input": ["banana"], "expected": "yes"}, {"input": ["grape"], "expected": "no"}]),
("Say Goodbye", 3, "fundamentals",
"Write a function called bye that prints \"Goodbye!\". Call it twice.",
["functions"],
[{"input": [], "expected": "Goodbye!\nGoodbye!"}]),
("Print a Border", 3, "fundamentals",
"Write a function called border that prints a line of equal signs. Call it, print \"Title\", then call it again.",
["functions"],
[{"input": [], "expected": "========\nTitle\n========"}]),
("Function Calling Function", 3, "fundamentals",
"Write a function a that prints \"A\", and a function b that calls a() and then prints \"B\". Call b().",
["functions"],
[{"input": [], "expected": "A\nB"}]),
("Count with a Function", 3, "fundamentals",
"Write a function called count_to_three that prints 1, 2, 3 (one per line) using a loop inside it. Call it.",
["functions"],
[{"input": [], "expected": "1\n2\n3"}]),
("Return a Value", 3, "fundamentals",
"Write a function called five that returns 5. Print the result of calling it twice and adding the results together.",
["functions"],
[{"input": [], "expected": "10"}]),
("Greet Function No Args", 3, "fundamentals",
"Write a function called greet that prints \"Hello there!\". Read a number and call greet() that many times.",
["functions"],
[{"input": ["2"], "expected": "Hello there!\nHello there!"}, {"input": ["1"], "expected": "Hello there!"}]),
("Function Returns Then Used", 3, "fundamentals",
"Write a function called square that returns 4 times 4. Print the result of calling it, minus 1.",
["functions"],
[{"input": [], "expected": "15"}]),
("Two Functions, One Output", 3, "fundamentals",
"Write two functions, line1 and line2, each printing a different line of text. Call line1() then line2().",
["functions"],
[{"input": [], "expected": "Line one\nLine two"}]),
("Function With a Loop and Return", 3, "fundamentals",
"Write a function called total_to_five that adds up 1 through 5 with a loop and returns the total. Print the result.",
["functions"],
[{"input": [], "expected": "15"}]),
("Call It Twice Or Once", 3, "fundamentals",
"Write a function called hi that prints \"Hi\". Read a number -- if it's greater than 1, call hi() twice, otherwise once.",
["functions"],
[{"input": ["2"], "expected": "Hi\nHi"}, {"input": ["1"], "expected": "Hi"}, {"input": ["5"], "expected": "Hi\nHi"}]),
("Triple It (Parameter)", 3, "fundamentals",
"Write a function called triple that takes one parameter and returns it times 3. Read a number, call triple(), and print the result.",
["parameters"],
[{"input": ["3"], "expected": "9"}, {"input": ["10"], "expected": "30"}]),
("Greet By Name (Parameter)", 3, "fundamentals",
"Write a function called greet that takes a name and prints \"Hi, \" followed by it. Read a name and call greet() with it.",
["parameters"],
[{"input": ["Sam"], "expected": "Hi, Sam"}]),
("Max of Two", 3, "fundamentals",
"Write a function called bigger that takes two parameters and returns whichever is larger. Read two numbers, call it, and print the result.",
["parameters"],
[{"input": ["5", "3"], "expected": "5"}, {"input": ["2", "8"], "expected": "8"}]),
("Repeat With a Parameter", 3, "fundamentals",
"Write a function called say_hi that takes a number and prints \"Hi\" that many times using a loop. Read a number and call it.",
["parameters"],
[{"input": ["3"], "expected": "Hi\nHi\nHi"}, {"input": ["1"], "expected": "Hi"}]),
("Multiply Two Params", 3, "fundamentals",
"Write a function called multiply that takes two parameters and returns their product. Read two numbers, call it, and print the result.",
["parameters"],
[{"input": ["4", "5"], "expected": "20"}, {"input": ["3", "3"], "expected": "9"}]),
("Is Even Parameter", 3, "fundamentals",
"Write a function called is_even that takes a number and returns whether it's even. Read a number, call it, and print the result.",
["parameters"],
[{"input": ["4"], "expected": "True"}, {"input": ["7"], "expected": "False"}]),
("Three Parameters", 3, "fundamentals",
"Write a function called add_three that takes three parameters and returns their sum. Read three numbers, call it, and print the result.",
["parameters"],
[{"input": ["1", "2", "3"], "expected": "6"}, {"input": ["10", "10", "10"], "expected": "30"}]),
("Shout Twice", 3, "fundamentals",
"Write a function called shout that takes a word and prints it followed by \"!!!\". Read a word and call shout() with it twice.",
["parameters"],
[{"input": ["go"], "expected": "go!!!\ngo!!!"}]),
("Subtract Params", 3, "fundamentals",
"Write a function called subtract that takes two parameters and returns the first minus the second. Read two numbers, call it, and print the result.",
["parameters"],
[{"input": ["10", "4"], "expected": "6"}, {"input": ["3", "9"], "expected": "-6"}]),
("Parameter Used in a Loop Range", 3, "fundamentals",
"Write a function called count_up that takes a number and prints 1 through it, one per line, using the parameter inside a loop.",
["parameters"],
[{"input": ["3"], "expected": "1\n2\n3"}, {"input": ["5"], "expected": "1\n2\n3\n4\n5"}]),
("Area of a Rectangle", 3, "fundamentals",
"Read a width and a height and print their product (the area).",
["simple_math"],
[{"input": ["4", "5"], "expected": "20"}, {"input": ["3", "3"], "expected": "9"}]),
("Subtract in Order", 3, "fundamentals",
"Read two numbers and print the first one minus the second one.",
["simple_math"],
[{"input": ["10", "3"], "expected": "7"}, {"input": ["2", "9"], "expected": "-7"}]),
("Square a Number", 3, "fundamentals",
"Read a number and print it multiplied by itself.",
["simple_math"],
[{"input": ["5"], "expected": "25"}, {"input": ["7"], "expected": "49"}]),
("Half of It", 3, "fundamentals",
"Read a whole number and print it divided by 2 using integer division (//).",
["simple_math"],
[{"input": ["10"], "expected": "5"}, {"input": ["7"], "expected": "3"}]),
("Is It Divisible", 3, "fundamentals",
"Read two numbers and print \"yes\" if the first is evenly divisible by the second, otherwise \"no\".",
["simple_math"],
[{"input": ["10", "5"], "expected": "yes"}, {"input": ["7", "2"], "expected": "no"}]),
("Sum of Three", 3, "fundamentals",
"Read three numbers and print their sum.",
["simple_math"],
[{"input": ["1", "2", "3"], "expected": "6"}, {"input": ["10", "20", "30"], "expected": "60"}]),
("Convert Celsius to Fahrenheit", 3, "fundamentals",
"Read a Celsius temperature and print it converted to Fahrenheit using f = c * 9 / 5 + 32.",
["simple_math"],
[{"input": ["0"], "expected": "32.0"}, {"input": ["100"], "expected": "212.0"}]),
("Exponent Practice", 3, "fundamentals",
"Read a base and an exponent and print the base raised to that power using **.",
["simple_math"],
[{"input": ["2", "3"], "expected": "8"}, {"input": ["3", "2"], "expected": "9"}]),
("Absolute Difference", 3, "fundamentals",
"Read two numbers and print the absolute value of their difference using abs().",
["simple_math"],
[{"input": ["3", "10"], "expected": "7"}, {"input": ["10", "3"], "expected": "7"}]),
("Perimeter of a Square", 3, "fundamentals",
"Read a side length and print the perimeter of a square with that side (4 times the side).",
["simple_math"],
[{"input": ["5"], "expected": "20"}, {"input": ["3"], "expected": "12"}]),
("Seeded Coin Flip", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.choice() between \"heads\" and \"tails\".",
["random_library"],
[{"input": ["1"], "expected": "heads"}, {"input": ["3"], "expected": "tails"}, {"input": ["6"], "expected": "tails"}]),
("Seeded Dice Roll", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.randint(1, 6).",
["random_library"],
[{"input": ["4"], "expected": "6"}, {"input": ["12"], "expected": "1"}, {"input": ["77"], "expected": "6"}]),
("Seeded Pick from Numbers", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.choice() on the list [10, 20, 30, 40].",
["random_library"],
[{"input": ["6"], "expected": "40"}, {"input": ["19"], "expected": "10"}, {"input": ["31"], "expected": "20"}]),
("Seeded Random Between", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.randint(1, 100).",
["random_library"],
[{"input": ["14"], "expected": "52"}, {"input": ["21"], "expected": "5"}, {"input": ["50"], "expected": "50"}]),
("Seeded Random Float", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.random().",
["random_library"],
[{"input": ["1"], "expected": "0.417022004702574"}, {"input": ["2"], "expected": "0.4359949021420038"}]),
("Seeded Pick a Letter", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.choice() on a list of 5 letters.",
["random_library"],
[{"input": ["7"], "expected": "a"}, {"input": ["13"], "expected": "d"}, {"input": ["25"], "expected": "e"}]),
("Seeded Random in Range", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.randint(1, 3).",
["random_library"],
[{"input": ["4"], "expected": "3"}, {"input": ["15"], "expected": "3"}, {"input": ["26"], "expected": "1"}]),
("Seeded Pick a Day", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.choice() on a list of weekday names.",
["random_library"],
[{"input": ["9"], "expected": "Mon"}, {"input": ["17"], "expected": "Tue"}, {"input": ["29"], "expected": "Fri"}]),
("Seeded Dice Twice", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Call random.randint(1, 6) twice and print both results, one per line.",
["random_library"],
[{"input": ["3"], "expected": "4\n5"}, {"input": ["16"], "expected": "2\n4"}]),
("Seeded Number Then Choice", 3, "fundamentals",
"Import random. Read a whole number and use it as a seed with random.seed(). Print the result of random.randint(1, 10), then the result of random.choice() on a list of 3 colors.",
["random_library"],
[{"input": ["5"], "expected": "3\nblue"}, {"input": ["18"], "expected": "7\ngreen"}]),
("Sign of a Number", 3, "fundamentals",
"Read a number and print \"positive\", \"negative\", or \"zero\" using if/elif/else.",
["conditionals"],
[{"input": ["5"], "expected": "positive"}, {"input": ["-3"], "expected": "negative"}, {"input": ["0"], "expected": "zero"}]),
("Grade From Score", 3, "fundamentals",
"Read a test score and print a letter grade: A for 90+, B for 80+, C for 70+, otherwise F. Use if/elif/else.",
["conditionals"],
[{"input": ["95"], "expected": "A"}, {"input": ["85"], "expected": "B"}, {"input": ["72"], "expected": "C"}, {"input": ["50"], "expected": "F"}]),
("Biggest of Three (If/Elif)", 3, "fundamentals",
"Read 3 numbers and print the largest one using if/elif/else (no max(), no list) -- compare them directly.",
["conditionals"],
[{"input": ["3", "9", "5"], "expected": "9"}, {"input": ["10", "2", "7"], "expected": "10"}, {"input": ["1", "2", "9"], "expected": "9"}]),
("Weekday or Weekend", 3, "fundamentals",
"Read a day name and print \"weekend\" if it's Saturday or Sunday, otherwise \"weekday\".",
["conditionals"],
[{"input": ["Saturday"], "expected": "weekend"}, {"input": ["Monday"], "expected": "weekday"}, {"input": ["Sunday"], "expected": "weekend"}]),
("Temperature Advice", 3, "fundamentals",
"Read a temperature and print \"It is hot\" (85+), \"It is nice\" (60+), or \"It is cold\", using if/elif/else.",
["conditionals"],
[{"input": ["90"], "expected": "It is hot"}, {"input": ["70"], "expected": "It is nice"}, {"input": ["40"], "expected": "It is cold"}]),
("Even, Odd, or Zero", 3, "fundamentals",
"Read a number and print \"zero\", \"even\", or \"odd\" -- check for zero before checking even/odd.",
["conditionals"],
[{"input": ["0"], "expected": "zero"}, {"input": ["4"], "expected": "even"}, {"input": ["7"], "expected": "odd"}]),
("Ticket Price by Age", 3, "fundamentals",
"Read an age and print the ticket price: 0 under 5, 10 under 18, otherwise 15. Use if/elif/else.",
["conditionals"],
[{"input": ["3"], "expected": "0"}, {"input": ["12"], "expected": "10"}, {"input": ["30"], "expected": "15"}]),
("Two Conditions With And", 3, "fundamentals",
"Read a number and print \"positive even\" if it's both greater than 0 AND even, otherwise \"not positive even\".",
["conditionals"],
[{"input": ["4"], "expected": "positive even"}, {"input": ["-4"], "expected": "not positive even"}, {"input": ["3"], "expected": "not positive even"}]),
("Login Check", 3, "fundamentals",
"Read a username and a PIN. Print \"access granted\" only if the username is \"admin\" AND the PIN is \"1234\", otherwise \"access denied\".",
["conditionals"],
[{"input": ["admin", "1234"], "expected": "access granted"}, {"input": ["admin", "0000"], "expected": "access denied"}, {"input": ["guest", "1234"], "expected": "access denied"}]),
("Rock Paper Scissors Winner", 3, "fundamentals",
"Read two moves (rock/paper/scissors) and print who wins: \"tie\", \"player 1 wins\", or \"player 2 wins\", using if/elif/else.",
["conditionals"],
[{"input": ["rock", "scissors"], "expected": "player 1 wins"}, {"input": ["paper", "rock"], "expected": "player 1 wins"}, {"input": ["rock", "paper"], "expected": "player 2 wins"}, {"input": ["rock", "rock"], "expected": "tie"}]),
("Stop At First Match", 3, "fundamentals",
"Loop from 1 upward and use break to stop and print the number the moment it equals the one read from input().",
["loops_breaks"],
[{"input": ["7"], "expected": "7"}, {"input": ["42"], "expected": "42"}]),
("Find First Multiple", 3, "fundamentals",
"Read a number n, loop upward from 1, and use break to stop at and print the very first multiple of n.",
["loops_breaks"],
[{"input": ["7"], "expected": "7"}, {"input": ["13"], "expected": "13"}]),
("Search a List and Stop", 3, "fundamentals",
"Given a fixed list of numbers, read a target and loop through the list, using break the moment you find it, then print whether it was found.",
["loops_breaks"],
[{"input": ["15"], "expected": "True"}, {"input": ["100"], "expected": "False"}]),
("Skip the Multiples of Three", 3, "fundamentals",
"Read a whole number n and print every number from 1 to n EXCEPT multiples of 3, using continue to skip them.",
["loops_breaks"],
[{"input": ["6"], "expected": "1\n2\n4\n5"}, {"input": ["9"], "expected": "1\n2\n4\n5\n7\n8"}]),
("Sum Until Negative", 3, "fundamentals",
"Read up to n numbers one at a time, adding each to a running total, but stop early with break the moment a negative number appears. Print the total.",
["loops_breaks"],
[{"input": ["5", "3", "4", "-1", "10", "10"], "expected": "7"}, {"input": ["3", "1", "2", "3"], "expected": "6"}]),
("Print Until a Word", 3, "fundamentals",
"Read up to n words one at a time and print each one, but stop early with break the moment the word \"stop\" appears.",
["loops_breaks"],
[{"input": ["4", "cat", "dog", "stop", "fish"], "expected": "cat\ndog"}, {"input": ["2", "hi", "bye"], "expected": "hi\nbye"}]),
("Only Print Positives", 3, "fundamentals",
"Read n numbers one at a time and print only the positive ones, using continue to skip zero or negative numbers.",
["loops_breaks"],
[{"input": ["4", "3", "-1", "0", "5"], "expected": "3\n5"}, {"input": ["3", "1", "2", "3"], "expected": "1\n2\n3"}]),
("First Even Number", 3, "fundamentals",
"Read numbers one at a time and use break to stop at and print the very first even number.",
["loops_breaks"],
[{"input": ["4", "3", "7", "8", "1"], "expected": "8"}, {"input": ["3", "2", "4", "6"], "expected": "2"}]),
("Countdown With an Early Stop", 3, "fundamentals",
"Count down from n, but use break to stop before printing a specific number read from input().",
["loops_breaks"],
[{"input": ["5", "2"], "expected": "5\n4\n3"}, {"input": ["10", "8"], "expected": "10\n9"}]),
("While Loop With Break", 3, "fundamentals",
"Use a while True loop with an if/break inside it to count up from 1 to n, one per line, instead of a for loop.",
["loops_breaks"],
[{"input": ["3"], "expected": "1\n2\n3"}, {"input": ["5"], "expected": "1\n2\n3\n4\n5"}]),
]
# Hand-picked, kid-appropriate vocabulary for generated login handles
# (e.g. "PinkPanther29") -- deliberately not an external name-generator
# package, so every possible word is one we've actually looked at.
HANDLE_ADJECTIVES = [
"Pink", "Blue", "Green", "Golden", "Silver", "Speedy", "Jumpy", "Silly",
"Clever", "Brave", "Mighty", "Sneaky", "Bouncy", "Fuzzy", "Sparkly",
"Cosmic", "Turbo", "Zippy", "Happy", "Lucky",
]
HANDLE_ANIMALS = [
"Panther", "Tiger", "Falcon", "Otter", "Dolphin", "Fox", "Wolf", "Panda",
"Koala", "Hedgehog", "Raccoon", "Penguin", "Narwhal", "Gecko", "Lynx",
"Badger", "Heron", "Marmot", "Cobra", "Puffin",
]
def get_db():
db = getattr(g, "_db", None)
if db is None:
db = g._db = dbmod.connect()
return db
@app.teardown_appcontext
def close_db(_exc):
db = getattr(g, "_db", None)
if db is not None:
db.close()
def _ensure_column(db, table, column, coldef):
cols = [r[1] for r in db.execute(f"PRAGMA table_info({table})").fetchall()]
if column not in cols:
db.execute(f"ALTER TABLE {table} ADD COLUMN {coldef}")
# The turtle building blocks were being taught without being named, so the
# tasks that already exercise them carry no tag for them. Match on what a
# brief actually says and append the skill -- additive, never replacing an
# existing tag, and skipped once present so repeated worker boots are a
# no-op.
RETAG_PATTERNS = {
"pen_control": r"pen ?up|pen ?down|lift the pen|pen off|pen ?size|\bdot\b",
"positioning": r"\bgoto\b|set ?pos|\bheading\b|set ?heading|\bteleport\b",
"modules": r"\bimport\b|\bmodule\b",
}
def _retag_tasks_for_new_skills(db):
import re as _re
changed = 0
for row in dbmod.fetchall(db.execute(
"SELECT id, title, brief, skills FROM tasks WHERE skills IS NOT NULL")):
try:
tags = json.loads(row["skills"] or "[]")
except (TypeError, ValueError):
continue
if not isinstance(tags, list):
continue
text = f"{row['title']} {row['brief']}".lower()
added = False
for skill, pattern in RETAG_PATTERNS.items():
if skill not in tags and _re.search(pattern, text):
tags.append(skill)
added = True
if added:
db.execute("UPDATE tasks SET skills=? WHERE id=?", (json.dumps(tags), row["id"]))
changed += 1
return changed
def _seed_missing_positioning_tasks(db):
"""Give existing kids the new primitive tasks.
seed_starter_tasks_for_kid only runs when a kid is created, so kids who
already exist would have skills on their map with nothing to practise
on. Keyed on title per kid so repeated worker boots don't duplicate.
"""
added = 0
for kid in dbmod.fetchall(db.execute("SELECT id FROM kids")):
for title, points, diff, brief, skills in POSITIONING_TASKS:
exists = dbmod.fetchone(db.execute(
"SELECT 1 FROM tasks WHERE kid_id=? AND title=?", (kid["id"], title)))
if exists:
continue
db.execute(
"""INSERT INTO tasks (id, title, points, difficulty, brief, kid_id, skills, status, vehicle, test_cases)
VALUES (?, ?, ?, ?, ?, ?, ?, 'active', 'turtle', NULL)""",
("t_" + uuid.uuid4().hex[:10], title, points, diff, brief, kid["id"],
json.dumps(skills)))
added += 1
return added
def init_db():
db = dbmod.connect()
db.executescript(
"""
CREATE TABLE IF NOT EXISTS families (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
created_at TEXT NOT NULL,
claim_code TEXT
);
CREATE TABLE IF NOT EXISTS parents (
id TEXT PRIMARY KEY,
family_id TEXT NOT NULL,
google_sub TEXT NOT NULL UNIQUE,
email TEXT NOT NULL,
display_name TEXT,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_parents_family_id ON parents(family_id);
CREATE TABLE IF NOT EXISTS kids (
id TEXT PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY,
title TEXT NOT NULL,
brief TEXT NOT NULL,
points INTEGER NOT NULL,
difficulty TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS submissions (
id TEXT PRIMARY KEY,
task_id TEXT NOT NULL,
title TEXT NOT NULL,
points INTEGER NOT NULL,
explanation TEXT,
code TEXT,
status TEXT NOT NULL DEFAULT 'pending',
review_note TEXT,
submitted_at TEXT NOT NULL,
reviewed_at TEXT
);
CREATE TABLE IF NOT EXISTS redemptions (
id TEXT PRIMARY KEY,
minutes INTEGER NOT NULL,
points INTEGER NOT NULL,
date TEXT NOT NULL,
redeemed_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS settings (
family_id TEXT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (family_id, key)
);
CREATE TABLE IF NOT EXISTS teams (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
code TEXT NOT NULL,
goal_points INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS idx_teams_code ON teams(code);
CREATE TABLE IF NOT EXISTS team_quests (
id TEXT PRIMARY KEY,
team_id TEXT NOT NULL,
season INTEGER NOT NULL,
goal_points INTEGER NOT NULL DEFAULT 0,
starts_at TEXT NOT NULL,
ends_at TEXT NOT NULL,
started_by TEXT,
created_at TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_team_quests_team ON team_quests(team_id);
"""
)
for table in ("tasks", "submissions", "redemptions"):
_ensure_column(db, table, "kid_id", "kid_id TEXT NOT NULL DEFAULT 'shayne'")
_ensure_column(db, "tasks", "source", "source TEXT NOT NULL DEFAULT 'seed'")
_ensure_column(db, "tasks", "status", "status TEXT NOT NULL DEFAULT 'active'")
_ensure_column(db, "tasks", "skills", "skills TEXT")
# Added mid-project (this session) but never covered by an
# _ensure_column call -- a genuinely fresh install would have crashed
# on GET /api/tasks (row_to_task reads both unconditionally).
_ensure_column(db, "tasks", "vehicle", "vehicle TEXT NOT NULL DEFAULT 'turtle'")
_ensure_column(db, "tasks", "test_cases", "test_cases TEXT")
_ensure_column(db, "submissions", "snapshot", "snapshot TEXT")
_ensure_column(db, "submissions", "pasted", "pasted INTEGER NOT NULL DEFAULT 0")
_ensure_column(db, "kids", "pin", "pin TEXT NOT NULL DEFAULT '0000'")
_ensure_column(db, "kids", "avatar", "avatar TEXT")
# Nullable: a kid may not have a birthday recorded, and row_to_kid
# reads it unconditionally, so this has to exist before any read.
_ensure_column(db, "kids", "birthday", "birthday TEXT")
# Multi-family additions -- nullable/defaulted so this is safe to run
# against a DB that hasn't been through migrate_multitenant.py yet
# (local fresh installs; the one production DB gets migrated
# separately, once, by that script rather than by booting new code
# against it un-migrated).
_ensure_column(db, "kids", "family_id", "family_id TEXT")
_ensure_column(db, "kids", "handle", "handle TEXT")
_ensure_column(db, "kids", "failed_pin_count", "failed_pin_count INTEGER NOT NULL DEFAULT 0")
_ensure_column(db, "kids", "pin_locked_until", "pin_locked_until TEXT")
# Cross-family teams -- deliberately the one place a kid's data is
# visible outside their own family (see /api/teams/me). Nullable: a
# kid is on at most one team at a time, enforced just by this being
# a single column rather than a join table.
_ensure_column(db, "kids", "team_id", "team_id TEXT")
# Guild quests run for a window rather than forever. Nullable so teams
# created before this still load; a missing window reads as "no quest
# running" rather than an error.
_ensure_column(db, "teams", "starts_at", "starts_at TEXT")
_ensure_column(db, "teams", "ends_at", "ends_at TEXT")
_ensure_column(db, "teams", "leader_kid_id", "leader_kid_id TEXT")
# Must run after the _ensure_column call above -- kids predates
# family_id (an existing table gaining a new column), unlike parents,
# which is created fresh with family_id already in its column list.
db.execute("CREATE INDEX IF NOT EXISTS idx_kids_family_id ON kids(family_id)")
_retag_tasks_for_new_skills(db)
_seed_missing_positioning_tasks(db)
# Guild quests used to be three mutable columns on the team row, so a
# new quest overwrote the old one and the previous result was simply
# forgotten. Seasons are rows instead. Fold any existing window into
# season 1 -- idempotent, since init_db runs on every worker boot.
# Every guild needs a leader. Existing ones never recorded a founder,
# so adopt whoever started season 1 if that was captured, and
# otherwise the first member -- deterministic, so repeated boots agree.
for t in dbmod.fetchall(db.execute(
"SELECT id FROM teams WHERE leader_kid_id IS NULL")):
starter = dbmod.fetchone(db.execute(
"""SELECT k.id AS id FROM team_quests q JOIN kids k
ON lower(k.handle) = lower(q.started_by)
WHERE q.team_id=? AND q.started_by IS NOT NULL
ORDER BY q.season ASC LIMIT 1""", (t["id"],)))
if not starter:
starter = dbmod.fetchone(db.execute(
"SELECT id FROM kids WHERE team_id=? ORDER BY id ASC LIMIT 1", (t["id"],)))
if starter:
db.execute("UPDATE teams SET leader_kid_id=? WHERE id=?", (starter["id"], t["id"]))
for t in dbmod.fetchall(db.execute(
"SELECT id, goal_points, starts_at, ends_at, created_at FROM teams "
"WHERE starts_at IS NOT NULL AND ends_at IS NOT NULL")):
existing = dbmod.fetchone(db.execute(
"SELECT 1 FROM team_quests WHERE team_id=?", (t["id"],)))
if existing:
continue
db.execute(
"""INSERT INTO team_quests (id, team_id, season, goal_points, starts_at, ends_at, started_by, created_at)
VALUES (?, ?, 1, ?, ?, ?, NULL, ?)""",
("tq_" + uuid.uuid4().hex[:10], t["id"], t["goal_points"],
t["starts_at"], t["ends_at"], t["created_at"]))
dbmod.commit_and_sync(db)
db.close()
row_to_task = dbmod.row_to_task
row_to_submission = dbmod.row_to_submission
row_to_redemption = dbmod.row_to_redemption
row_to_kid = dbmod.row_to_kid
# ---------------- Auth helpers ----------------
def require_family_session(fn):
"""Any logged-in identity (kid or parent) within a family. Populates
g.family_id (always), g.kid_id / g.parent_id (whichever role is
active, the other is None) -- route bodies use these, never a
client-supplied value, to decide what's visible/touchable."""
@wraps(fn)
def wrapper(*args, **kwargs):
family_id = session.get("family_id")
if not family_id:
return jsonify({"error": "not logged in"}), 401
g.family_id = family_id
g.kid_id = session.get("kid_id")
g.parent_id = session.get("parent_id")
return fn(*args, **kwargs)
return wrapper
def require_parent_login(fn):
"""Parent-only actions (task/submission/kid/settings management)."""
@wraps(fn)
@require_family_session
def wrapper(*args, **kwargs):
if not g.parent_id:
return jsonify({"error": "parent login required"}), 403
return fn(*args, **kwargs)
return wrapper
def _kid_family_id(db, kid_id):
row = dbmod.fetchone(db.execute("SELECT family_id FROM kids WHERE id=?", (kid_id,)))
return row["family_id"] if row else None
def _task_family_id(db, task_id):
row = dbmod.fetchone(db.execute(
"SELECT kids.family_id AS family_id FROM tasks JOIN kids ON kids.id = tasks.kid_id WHERE tasks.id=?",
(task_id,),
))
return row["family_id"] if row else None
def _submission_family_id(db, sub_id):
row = dbmod.fetchone(db.execute(
"SELECT kids.family_id AS family_id FROM submissions JOIN kids ON kids.id = submissions.kid_id WHERE submissions.id=?",