These questions follow Variables and Data Types and
Input and Output. A variable is a name for a value; a type is what
that value is allowed to do. Almost every early bug in this course is
one of those two sentences being ignored.
Questions
Give the type β int, float, str, or bool β for each: the
number of chairs in a room; the mass of a recycling bin in
kilograms; a book title; whether a permission form is in.
Predict the output.
rows = 8rows = rows + 4print(rows)
Predict the output. Explain the quotation marks.
print(type("14"))
Find the fault. The club advisor types 6.5 and this crashes.
Rename these so the next reader does not have to guess:
x = 12, n = "Priya", flag = True.
Write a program that asks for a name and a number of days, then
prints Priya, the trip is in 14 days.
Challenge. A coach has total_minutes = 250. Print it as
4 h 10 min, without using a decimal point anywhere.
Answers
Answer 1
int for chairs (you count them, and half a chair is not a thing),
float for kilograms (measurements have decimals), str for a
title, bool for whether the form is in. If you were tempted by
str for the mass, remember that text cannot be added up.
Answer 2
12. The second line is not a contradiction β = means βwork out
the right-hand side and store it in the name on the leftβ. Python
computes 8 + 4 first, then puts 12 back into rows.
Answer 3
<class 'str'>. The quotation marks make it text, not a number.
"14" and 14 look identical when printed and behave completely
differently: one can be added to 1 and one cannot.
Answer 4
input() always returns text, so answer is the string "6.5", and
Python refuses to add a number to text:
TypeError: can only concatenate str (not "float") to str
float, not int β int("6.5") raises ValueError, because int
only accepts text that spells a whole number.
Answer 5
3.1. The division gives 3.0833333333333335; :.1f rounds the
display to one decimal place. The value stored is unchanged β
formatting is about the reader, not the arithmetic.
Answer 6
Something like chairs_per_row = 12, player_name = "Priya",
form_returned = True. Boolean names read best as a claim that is
either true or false, so form_returned beats flag and beats
is_form too.
Answer 7
name = input("Name: ")days = int(input("Days until the trip: "))print(f"{name}, the trip is in {days} days.")
Name: Priya
Days until the trip: 14
Priya, the trip is in 14 days.
The int() is not strictly needed to print the number β but the
moment anybody wants to subtract from it, text will not do.
Answer 8
total_minutes = 250print(f"{total_minutes // 60} h {total_minutes % 60} min")
4 h 10 min
// divides and discards the remainder; % keeps only the
remainder. Ordinary / would give 4.1666... hours, which is a
different and less useful sentence.