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 True or False. That makes them strings, so the next problem's type() test reports str instead of bool.
  • Writing true or false in lowercase. Python will raise a NameError because true is 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 word str. 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 = True

A 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 False or None. Python raises a SyntaxError because 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__)   # str

Common mistakes.

  • Writing none in lowercase. Python raises a NameError; the correct spelling is None with a capital N.
  • Thinking None is the same as False. They are different types. The related but separate idea of "truthy and falsy" comes in Chapter 16 on if.

Done?

Next — Strings — digs into the type you have used most. Then numbers, keyboard input, and the Part 3 mini-project.