Python and AI for children: write your first program
Build a quiz with a Python function and condition, then test it with three different answers.
Why start with Python?
People use Python to automate work, analyse data and write server applications. In a short script, many lines read like instructions. Indentation shows which lines belong inside a function or condition. Save the example as quiz.py and run python quiz.py in an environment with Python installed.
Variables, conditions and functions
The variable answer holds the typed text. The score function receives it and returns one point if it matches, or zero otherwise. The strip and lower methods remove spaces at the ends and convert letters to lowercase. Try Mars, the same answer surrounded by spaces and an empty answer.
def score(answer):
if answer.strip().lower() == "mars":
return 1
return 0
answer = input("Which planet is known as the Red Planet? ")
print(score(answer))Debug one thing at a time
If the script fails to run, read the last line of the error and look up the line number. Check the colon after if and indentation before return. Ask AI: “Explain this error in two sentences and give me one hint so I can fix it myself.” If the program runs but scores incorrectly, write down the expected and actual results.
Make it your own
Add a second question with a different correct answer. Pass the correct answer as another function parameter and add both scores. Try all four combinations of right and wrong answers. Ask your child to explain why the same function can be used twice.
Related reading
Related courses
Lessons are taught in Ukrainian or Russian.
Free one-to-one trial lesson