Why might a school fair program turn 2 and 3 tickets into 23?
At the school fair, one group buys two tickets and another buys three. You type the numbers into a Python program, but it gives you twenty-three tickets. How can two plus three give you that?
You already know how to join two strings. Watch what happens when you join the text School and the text Fair. The space at the end of School stays there. That joining operation is called concatenation.
Python's input function returns a string, even when you type a number. On the left, int converts the text twelve to the integer twelve. On the right, float converts the text twelve point five to a number you can calculate with. Python uses the float type to represent real numbers.
You type eight point five into a Python program, and input stores it in a variable called entry. You need to use the value in a calculation. Which expression gives you the number eight point five? Pick one.
Use int to turn the text twelve into an integer. Add three laps. Convert fifteen to text with str, then join it to the laps label. For distance, use float on the text twelve point five and add two point five. Convert fifteen point zero with str, then join it to the distance label.
Use int on the lap entry, nine, and float on the distance entry, six point five. Add three laps and two point five kilometres. Convert each result back to a string with str and join it to its label. Pick the pair of messages Python gives you.
On the left, the laps label is a string and fifteen is an integer. Python raises a type error when you try to join them with plus. On the right, str converts fifteen to a string, so plus joins the label and the result.
If the exam asks you to state the value stored in each variable, check the types on each line. Plus joins the strings two and three to give the text twenty-three. It adds the integers to give five. Finally, str turns five into text so you can join it to the label.