Lab 1 of 5about 25 minutes
Score a chatbot's answers
It uses what you learned in Notebooks and environments, Your first lines of Python and Lists, loops and if.
1The answers
Run itFrom Your first lines of Python and Lists, loops and if
You ask a chatbot the same question six times: how many legs does a spider have? It does not always give the same answer. Run the cell. The last line shows the first answer, as the memory keeps it.
The quotes tell you what it is: text, not a number. A chatbot always answers in text.
2One answer by its position
Your turnFrom Lists, loops and if
Keep the fifth answer under the name fifth. Count the positions as Python does.
Hint
The first answer is at position 0, so the fifth is one less than five.
One way to do it
fifth = answers[4]
fifth3How many legs too many?
Your turnFrom Your first lines of Python
The chatbot said 10, so it is 2 legs over. Try extra = fifth - 8 in the cell and run it. Python stops: TypeError: unsupported operand type(s) for -: 'str' and 'int'. Str is text and int is a whole number: you can't take a number away from text. Mend the line, so extra is a number.
Hint
One function turns text into a whole number. Call it on fifth.
One way to do it
extra = int(fifth) - 8
extra4Count the right answers
Your turnFrom Your first lines of Python and Lists, loops and if
Now go through every answer and count the ones that say 8. Two new things are given. == asks whether two values are the same, and gives True or False, as > does. And right = right + 1 works out the right side first, as every = does, so it keeps one more than before. Fill in the test.
Hint
The loop gives each answer the name answer, and each one is text. Do what you did in step 3.
One way to do it
right = 0
for answer in answers:
if int(answer) == 8:
right = right + 1
right5Ask again
Run itFrom Notebooks and environments
You ask six more times, and this cell keeps the new answers under the same name. Five of them say 8 now. Run it.
Still 4. The memory holds what you ran, and the cell that counts has not run since the answers changed. Run step 4's cell again, and right becomes 5. Before you trust a notebook, restart and run all: the button at the top runs every cell from a fresh memory, in order.
6An ant
On your ownFrom Your first lines of Python and Lists, loops and if
Now a new question: how many legs does an ant have? The right answer is 6. Keep these answers under the name ant_answers, then count the right ones into ant_right.
ant_answers = ["6", "6", "8", "6", "4", "6", "6"]
Hint
Step 4 is the plan. Change the list's name, the name of the count, and the right answer.
One way to do it
ant_answers = ["6", "6", "8", "6", "4", "6", "6"]
ant_right = 0
for answer in ant_answers:
if int(answer) == 6:
ant_right = ant_right + 1
ant_rightRun on Pyodide 314, Python 3.14. Checked on .
