{"base":{"id":"cs8-24","subject":"Programming","title":"Using libraries","scene":"pycode","steps":[{"mode":"why","eyebrow":"Libraries","title":"Don't reinvent the wheel","body":"A library is a collection of ready-made, tested routines. Making random numbers yourself takes tricky code; a library does it in one line. Compare the two programs.","actions":[{"id":"own","label":"Write it yourself"},{"id":"lib","label":"Use a library"}]},{"mode":"dice","eyebrow":"Calling a function","title":"Roll the dice","body":"import random loads the library; random.randint(1, 6) calls one of its functions and gives back a whole number from 1 to 6. Roll once, then roll 100 times.","actions":[{"id":"roll","label":"Run it: roll once"},{"id":"roll100","label":"Roll 100 times"}]},{"mode":"game","eyebrow":"In a game","title":"A new question every time","body":"Games use random numbers so they are different every time. This maths room makes a new times-table question with two calls to randint. Ask for new questions.","actions":[{"id":"new","label":"New question"}]},{"mode":"quiz","eyebrow":"Check yourself","title":"Which line?","body":"Sara wants her program to pick a random number from 1 to 100.","question":"Which line should she use after import random?","options":["n = randint","import 100","n = random.randint(1, 100)"],"answer":2,"right":"Correct. randint(1, 100) returns a whole number from 1 to 100.","wrong":"Not quite. Which line calls the library's function with the range?","look":"dice"}],"more":[{"type":"cards","id":"ideas","eyebrow":"Popular idea vs science","title":"Common mix-ups","items":[{"tag":"Popular idea","title":"\"Using a library is cheating.\"","body":"Professional programmers use libraries all the time. It saves time and avoids bugs."},{"tag":"Popular idea","title":"\"randint(1, 6) can give 0 or 7.\"","body":"Both ends are included: it gives 1, 2, 3, 4, 5 or 6."},{"tag":"Popular idea","title":"\"You must understand a library's code to use it.\"","body":"You need to know what it does, what to give it and what it gives back."}]},{"type":"cards","id":"libs","eyebrow":"Useful libraries","title":"Python libraries to try","items":[{"tag":"random","title":"Chance","body":"randint(1, 6) for dice; choice([\"rock\", \"paper\", \"scissors\"]) to pick one item."},{"tag":"math","title":"Maths","body":"math.sqrt(49) gives 7.0; math.pi gives 3.14159…"},{"tag":"time","title":"Timing","body":"time.sleep(2) pauses the program for 2 seconds."},{"tag":"turtle","title":"Drawing","body":"Draw shapes with a moving turtle, like the sequence lesson."}]},{"type":"cards","id":"code","eyebrow":"Code","title":"A complete dice game","items":[{"tag":"Python","title":"Two players","body":"import random\nyou = random.randint(1, 6)\nme = random.randint(1, 6)\nprint(\"You:\", you, \"Me:\", me)\nif you > me:\n    print(\"You win!\")\nelif me > you:\n    print(\"I win!\")\nelse:\n    print(\"A draw\")","code":true}]},{"type":"activities","id":"try","eyebrow":"Try it at home","title":"Rock, paper, scissors","items":[{"title":"Random opponent","time":"25 minutes","need":["A computer with Python (or an online editor)","An adult's permission"],"steps":["Type: import random, then comp = random.choice([\"rock\", \"paper\", \"scissors\"]).","Ask the player for their choice with input().","Use if/elif/else to decide who wins.","Play 10 times. Does the computer pick each option about equally often?"],"why":"random.choice is a ready-made library function; you only wrote the game rules.","safety":"Ask an adult before installing anything."}]},{"type":"quiz","id":"quiz","eyebrow":"Quiz","title":"Six more questions","questions":[{"question":"What is a library?","options":["A collection of ready-made routines","A room with books only","A type of variable"],"answer":0,"right":"Correct. Reusable, tested code.","wrong":"Not quite. Think about reusing code."},{"question":"What does import random do?","options":["Picks a number","Loads the random library","Deletes random files"],"answer":1,"right":"Correct. It makes the library's functions available.","wrong":"Not quite. What must happen before calling randint?"},{"question":"Which numbers can randint(1, 3) give?","options":["0, 1 or 2","1 or 2","1, 2 or 3"],"answer":2,"right":"Correct. Both ends are included.","wrong":"Not quite. Are both ends included?"},{"question":"Why do games use random numbers?","options":["To slow them down","So each game is different","To save storage"],"answer":1,"right":"Correct. Variety keeps games interesting.","wrong":"Not quite. What would happen without randomness?"},{"question":"Which is a benefit of libraries?","options":["Programs always get longer","Fewer bugs, because the code is tested","No need to test your own code"],"answer":1,"right":"Correct. Many people have used and tested them.","wrong":"Not quite. Who tested the library?"},{"question":"math.sqrt(81) gives…","options":["9.0","81","6561"],"answer":0,"right":"Correct. The square root of 81 is 9.","wrong":"Not quite. Which number times itself is 81?"}]},{"type":"checklist","id":"check","eyebrow":"Self-check","title":"I can explain…","items":["what a library is and why programmers use them","how to import a library and call a function","what random.randint(a, b) returns","how random numbers make games different each time"]},{"type":"keywords","id":"words","eyebrow":"Key words","title":"Words to know","items":[{"word":"library","meaning":"A collection of ready-made, tested routines."},{"word":"module","meaning":"Python's name for a library file."},{"word":"import","meaning":"Load a library so its functions can be used."},{"word":"function","meaning":"A named routine that does a job and may give back a value."},{"word":"call","meaning":"Run a function."}]},{"type":"sources","id":"sources","title":"Sources","items":["Python documentation: random module"]}]},"overlay":null}