11. Variables and types — Homework solutions
The .py solution files are in
exercises/11/homework/solutions/. The walkthroughs below
explain the thinking.
Problem 1 — Player vitals
Problem. Declare four variables of the right types and print each with a label.
How to think about it. Four declarations, four print
lines. Match types to values: a string for name, whole
numbers for level and hit_points, a boolean
for alive. Each label is a plain string, combined with an
f-string or passed as a second argument to print.
Worked solution.
name = "Keiko"
level = 7
hit_points = 95
alive = True
print(f"Name: {name}")
print(f"Level: {level}")
print(f"Hit points: {hit_points}")
print(f"Alive: {alive}")The f-string converts the boolean True to the text
True automatically, so no extra conversion step is
needed.
The comma form also works:
print("Alive:", alive)Common mistakes.
- Quoting
TrueorFalse. That makes them strings, so the next problem'stype()test reportsstrinstead ofbool. - Writing
trueorfalsein lowercase. Python will raise aNameErrorbecausetrueis not a recognised name.
Problem 2 — Type checker
Problem. Print type(x).__name__ of five
values.
How to think about it. Five print
calls, each wrapping type().__name__ around a literal; no
variable needed.
Worked solution.
print(type("world").__name__)
print(type(42).__name__)
print(type(3.14).__name__)
print(type(False).__name__)
print(type(None).__name__)The output is:
str
int
float
bool
NoneType
Common mistakes.
- Using
type(x)without.__name__. That prints<class 'str'>instead of the plain wordstr. Both describe the same thing, but.__name__is cleaner for display. - Wrapping the call in quotes:
print("type(42).__name__"). That prints the literal text. Without quotes, Python runs it and prints the type name.
Problem 3 — Rename and reassign
Problem. Rename three poorly named variables, print, reassign, print again.
How to think about it. This is about reading code.
The starter values hint at each variable's meaning:
a = "Sword of Light" is an item name, so rename it
item_name. Then reassign and print.
Worked solution. Given a starter:
a = "Sword of Light"
b = 12
c = TrueA reasonable rewrite:
item_name = "Sword of Light"
item_level = 12
is_equipped = True
print("Before:", item_name, item_level, is_equipped)
item_name = "Shield of Ages"
item_level = 5
is_equipped = False
print("After: ", item_name, item_level, is_equipped)Common mistakes.
- Naming a variable after a reserved word like
FalseorNone. Python raises aSyntaxErrorbecause those names have special meaning.
Challenge — The None mystery
Problem. Assign None to a variable,
confirm its type, then assign a value and confirm the type changed.
How to think about it. Assign None
directly: treasure = None. Print
type(treasure).__name__, set
treasure = "Gold Coin", then print the type again.
Worked solution.
# None is Python's way of saying "there is no value here at all".
# It is not the same as 0 or "" or False. It is the absence of a value.
# A variable can be assigned None explicitly to show it has no
# meaningful value yet.
treasure = None
print("Before assignment:", type(treasure).__name__) # NoneType
treasure = "Gold Coin"
print("After assignment: ", type(treasure).__name__) # strCommon mistakes.
- Writing
nonein lowercase. Python raises aNameError; the correct spelling isNonewith a capital N. - Thinking
Noneis the same asFalse. They are different types. The related but separate idea of "truthy and falsy" comes in Chapter 16 onif.
Done?
Next — Strings — digs into the type you have used most. Then numbers, keyboard input, and the Part 3 mini-project.