Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 4: Decisions

65 problems · 0 figures

Concepts in Practice 4.1.1 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
What is the data type of is_fruit?
  1. a.
    Boolean
  2. b.
    integer
  3. c.
    string

Answer

C. string
"True" uses quotation marks, so the value is a string.

Concepts in Practice 4.1.2 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
What is the data type of is_vegetable?
  1. a.
    Boolean
  2. b.
    integer
  3. c.
    string

Answer

B. integer
is_vegetable is an integer and can be used in arithmetic operations.

Concepts in Practice 4.1.3 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
What is the data type of is_dessert?
  1. a.
    Boolean
  2. b.
    integer
  3. c.
    string

Answer

A. Boolean
A variable assigned with either True or False is a Boolean variable.

Concepts in Practice 4.1.4 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
How many values can a Boolean variable represent?
  1. a.
    2
  2. b.
    4
  3. c.
    8

Answer

A. 2
A Boolean variable represents the value True or False .

Concepts in Practice 4.1.5 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
Which is a valid value for a Boolean variable?
  1. a.
    true
  2. b.
    True
  3. c.
    1

Answer

B. True
True and False are the only two values for a Boolean variable.

Concepts in Practice 4.1.6 §4.1 source ↗

Consider the following code:

is_fruit = "True"
is_vegetable = 0
is_dessert = False
Suppose the following is added to the code above:
is_dessert = 0
print(type(is_dessert))
What is the output?
  1. a.
    <class 'bool'>
  2. b.
    <class 'int'>
  3. c.
    Error
Video · Example: Crosswalk sign in the book ↗

Answer

B. <class 'int'>
Assigning a bool with an int results in implicit type conversion. The type conversion can cause issues later in the code if the programmer assumes is_dessert is still a Boolean.

Concepts in Practice 4.1.7 §4.1 source ↗

bool(0.000)
  1. a.
    True
  2. b.
    False

Answer

B. False
Any numeric type representing 0 converts to False . Non-zero numbers convert to True .

Concepts in Practice 4.1.8 §4.1 source ↗

bool(-1)
  1. a.
    True
  2. b.
    False

Answer

A. True
A negative number is non-zero and converts to True .

Concepts in Practice 4.1.9 §4.1 source ↗

bool("")
  1. a.
    True
  2. b.
    False

Answer

B. False
The empty string is the only string that converts to False .

Concepts in Practice 4.1.10 §4.1 source ↗

bool("0")
  1. a.
    True
  2. b.
    False

Answer

A. True
"0" is a non-empty string and is converted to True .

Concepts in Practice 4.1.11 §4.1 source ↗

Given input False, what is bool(input())?
  1. a.
    True
  2. b.
    False

Answer

A. True
The input is read in as a string, so bool("False") is True .

Concepts in Practice 4.1.12 §4.1 source ↗

Given is_on = True, what is the value of each expression?

float(is_on)
  1. a.
    0.0
  2. b.
    1.0

Answer

B. 1.0
True converted to a float is 1.0. False converted to a float is 0.0.

Concepts in Practice 4.1.13 §4.1 source ↗

Given is_on = True, what is the value of each expression?

str(is_on)
  1. a.
    "is_on"
  2. b.
    "True"

Answer

B. "True"
True converted to a string is "True" . False converted to a string is "False" .

Concepts in Practice 4.1.14 §4.1 source ↗

Given is_on = True, what is the value of each expression?

int(is_on)
  1. a.
    0
  2. b.
    1

Answer

B. 1
When a Boolean is converted to a numeric type, True is converted to 1 , and False is converted to 0 .

Concepts in Practice 4.1.15 §4.1 source ↗

For each new variable, what is the value of compare_result?

x = 14
compare_result = (x <= 13)
  1. a.
    True
  2. b.
    False

Answer

B. False
14 is greater than 13, so 14 is not less than or equal to 13.

Concepts in Practice 4.1.16 §4.1 source ↗

For each new variable, what is the value of compare_result?

w = 0
compare_result = (w != 0.4)
  1. a.
    True
  2. b.
    False

Answer

A. True
0 is less than 0.4, so 0 is not equal to 0.4.

Concepts in Practice 4.1.17 §4.1 source ↗

For each new variable, what is the value of compare_result?

v = 4
compare_result = (v < 4.0)
  1. a.
    True
  2. b.
    False

Answer

B. False
4 is equal to 4.0, so 4 is not less than 4.0.

Concepts in Practice 4.1.18 §4.1 source ↗

For each new variable, what is the value of compare_result?

y = 2
compare_result = (y > "ab")
  1. a.
    True
  2. b.
    False
  3. c.
    Error

Answer

C. Error
Numeric types cannot be compared to strings using the comparison operators: >, <, >=, <=. Using == or != will produce a Boolean.

Concepts in Practice 4.1.19 §4.1 source ↗

For each new variable, what is the value of compare_result?

z = "cilantro"
compare_result = (z == "coriander")
  1. a.
    True
  2. b.
    False

Answer

B. False
Strings are compared by comparing each character's Unicode values. Letters have ascending Unicode values in alphabetical order (and are case sensitive). "c" and "c" are equal, so "i" is compared to "o" . "i" is not equal to "o" so False is produced, though cilantro and coriander come from the same plant.

Concepts in Practice 4.1.20 §4.1 source ↗

For each new variable, what is the value of compare_result?

a = "dog"
compare_result = (a < "cat")
  1. a.
    True
  2. b.
    False

Answer

B. False
Letters have ascending Unicode values in alphabetical order (and are case sensitive). "d" is greater than, not less than, "c" , so False is produced.

Concepts in Practice 4.2.1 §4.2 source ↗

Given the following, which part is the condition?
if age < 12:
  print("Discount for children available")
  1. a.
    age
  2. b.
    age < 12
  3. c.
    print("Discount for children available")

Answer

B. age < 12
age < 12 is between "if" and ":" and is an expression that evaluates to True or False .

Concepts in Practice 4.2.2 §4.2 source ↗

Given the following, which lines execute if the condition is true?
1  print("Have a great day.")
2  if is_raining:
3    print("Don't forget an umbrella!")
4  print("See you soon.")
  1. a.
    1, 2, 3
  2. b.
    1, 2, 4
  3. c.
    1, 2, 3, 4

Answer

C. 1, 2, 3, 4
The program executes the body when the condition is true.

Concepts in Practice 4.2.3 §4.2 source ↗

Given the following (same as above), which lines execute if the condition is False?
1  print("Have a great day.")
2  if is_raining:
3    print("Don't forget an umbrella!")
4  print("See you soon.")
  1. a.
    1, 2, 3
  2. b.
    1, 2, 4
  3. c.
    1, 2, 3, 4
Video · Example: Quantity check in the book ↗

Answer

B. 1, 2, 4
The body, line 3, isn't executed because the condition is false.

Concepts in Practice 4.2.4 §4.2 source ↗

Given num = -10, what is the final value of num?
if num < 0:
  num = 25
if num < 100:
  num = num + 50
  1. a.
    -10
  2. b.
    40
  3. c.
    75

Answer

C. 75
-10 < 0 is true, so num is assigned with 25 . Then 25 < 100, so num is assigned with 25 + 50, which is 75 .

Concepts in Practice 4.2.5 §4.2 source ↗

Given input 10, what is the final value of positive_num?
positive_num = int(input("Enter a positive number:"))
if positive_num < 0:
  print("Negative input set to 0")
positive_num = 0
  1. a.
    10
  2. b.
    0
  3. c.
    Error

Answer

B. 0
Line 4 is not indented, so positive_num is always assigned with 0 . The positive input value is lost.

Concepts in Practice 4.2.6 §4.2 source ↗

Given the following code, the else branch is taken for which range of x?
if x >= 15:
  # Do something
else:
  # Do something else
  1. a.
    x >= 15
  2. b.
    x <= 15
  3. c.
    x < 15

Answer

C. x < 15
The if branch is taken when x >= 15, so the else branch is taken when x is not greater than or equal to 15 .

Concepts in Practice 4.2.7 §4.2 source ↗

Given x = 40, what is the final value of y?
if x > 30:
  y = x - 10
else:
  y = x + 10
  1. a.
    30
  2. b.
    40
  3. c.
    50

Answer

A. 30
40 > 30, so the body of the if statement is executed. y = 40 - 10 = 30.

Concepts in Practice 4.2.8 §4.2 source ↗

Given y = 50, which is not a possible final value of y?
if x < 50:
  y = y / 2
else:
  y = y * 2
y = y + 5
  1. a.
    30
  2. b.
    55
  3. c.
    105

Answer

B. 55
One of the branches is always taken in an if-else statement. Depending on x's value, y will either have a final value of 30 or 105 .

Concepts in Practice 4.3.1 §4.3 source ↗

Consider the example above. Jaden tries to enter when the capacity is 2500 and there are 2 hours before close. Can Jaden enter?
  1. a.
    yes
  2. b.
    no
Video · Example: Museum entry in the book ↗

Answer

A. yes
2500 < 3000 is True , and 2.5 > 1.0 is True , so Jaden can enter.

Concepts in Practice 4.3.2 §4.3 source ↗

Consider the example above. Darcy tries to enter when the capacity is 3000. For what values of hrs_to_close will Darcy to be able to enter?
  1. a.
    hrs_to close > 1.0
  2. b.
    no such value
Video · Example: Museum entry in the book ↗

Answer

B. no such value
3000 > 3000 is False . False and True is False . False and False is False . So no value of hrs_to_close will allow Darcy to enter.

Concepts in Practice 4.3.3 §4.3 source ↗

Given is_admin = False and is_online = True, what is the value of is_admin and is_online?
  1. a.
    True
  2. b.
    False

Answer

B. False
False and True is False .

Concepts in Practice 4.3.4 §4.3 source ↗

Given x = 8 and y = 21, what is the final value of z?
if (x < 10) and (y > 20):
  z = 5
else:
  z = 0
  1. a.
    0
  2. b.
    5

Answer

B. 5
(8 < 10) and (21 > 20) evaluates to True and True, which is True . So the body of the if is executed, and z is assigned with 5 .

Concepts in Practice 4.3.5 §4.3 source ↗

Given days = 21 and is_damaged is False, is the refund processed?
if (days < 30) or is_damaged:
  # Process refund
  1. a.
    yes
  2. b.
    no

Answer

A. yes
(21 < 30) or False evaluates to True or False, which is True .

Concepts in Practice 4.3.6 §4.3 source ↗

For what values of age is there no discount?
if (age < 12) or (age > 65):
  # Apply student/senior discount
  1. a.
    age >= 12
  2. b.
    age <= 65
  3. c.
    (age >= 12) and (age <= 65)

Answer

C. (age >= 12) and (age <= 65)
The discount does not apply for ages between 12 and 65 inclusive.

Concepts in Practice 4.3.7 §4.3 source ↗

Given a = 9 and b = 10, does the test pass?
if (a%2 == 0 and b%2 == 1) or (a%2 == 1 and b%2 == 0):
  # Test passed
else:
  # Test failed
  1. a.
    yes
  2. b.
    no

Answer

A. yes
(9%2==0 and 10%2 ==1) evaluates to (False and False), which is False . (9%2 == 1 and 10%2 == 0) evaluates to (True and True), which is True . False or True is True . The if statement checks whether the numbers form an even-odd or odd-even pair.

Concepts in Practice 4.3.8 §4.3 source ↗

Given x = 13, what is the value of not(x < 10)?
  1. a.
    True
  2. b.
    False

Answer

A. True
not(13 < 10) evaluates to not(False), which is True .

Concepts in Practice 4.3.9 §4.3 source ↗

Given x = 18, is x in the correct range?
if not(x > 15 and x < 20):
  # x in correct range
  1. a.
    yes
  2. b.
    no

Answer

B. no
not(18 > 15 and 18 < 20) evaluates to not(True and True), then not(True), and finally, False .

Concepts in Practice 4.3.10 §4.3 source ↗

Given is_turn = False and timer = 65, what is the final value of is_turn?
if timer > 60:
  is_turn = not is_turn
  1. a.
    True
  2. b.
    False

Answer

A. True
65 > 60 is True , so is_turn is assigned with not(False), which is True .

Concepts in Practice 4.4.1 §4.4 source ↗

Which part of each expression is evaluated first?

x ** 2 + 6 / 3
  1. a.
    6 / 3
  2. b.
    x ** 2
  3. c.
    2 + 6

Answer

B. x ** 2
Exponentiation has the highest precedence, then division, then addition.

Concepts in Practice 4.4.2 §4.4 source ↗

Which part of each expression is evaluated first?

not 3 * 5 > 10
  1. a.
    3 * 5
  2. b.
    not 3
  3. c.
    5 > 10

Answer

A. 3 * 5
Multiplication has the highest precedence, then greater than, then not.

Concepts in Practice 4.4.3 §4.4 source ↗

Which part of each expression is evaluated first?

z == 5 and x / 8 < 100
  1. a.
    5 and x
  2. b.
    x / 8
  3. c.
    8 < 100

Answer

B. x / 8
Division has the highest precedence, then equality and less than, then and .

Concepts in Practice 4.4.4 §4.4 source ↗

How is each expression evaluated?

10 + 3 * 2 / 4
  1. a.
    10 + (3 * (2 / 4))
  2. b.
    10 + ((3 * 2) / 4)
  3. c.
    (10 + 3) * (2 / 4)

Answer

B. 10 + ((3 * 2) / 4)
Multiplication and division have the highest precedence, are left associative, and have higher precedence than addition.

Concepts in Practice 4.4.5 §4.4 source ↗

How is each expression evaluated?

2 * 2 ** 2 ** 3
  1. a.
    2 * ((2 ** 2) ** 3)
  2. b.
    2 * (2 ** (2 ** 3))
  3. c.
    ((2*2) ** 2) ** 3

Answer

B. 2 * (2 ** (2 ** 3))
Exponentiation is right associative and has higher precedence than multiplication.

Concepts in Practice 4.4.6 §4.4 source ↗

How is each expression evaluated?

100 < x > 150
  1. a.
    100 < x and x < 150
  2. b.
    100 < x or x > 150
  3. c.
    100 < x and x > 150

Answer

C. 100 < x and x > 150
The expression is valid and is separated into comparisons connected with and .

Concepts in Practice 4.4.7 §4.4 source ↗

Consider the example above. Why was the evaluation order different from what the programmer wanted?
  1. a.
    Equality has precedence over and.
  2. b.
    All operators are evaluated right to left.
  3. c.
    Order is random when parentheses aren't used.
Video · Using parentheses in the book ↗

Answer

A. Equality has precedence over and.
The equality operator, ==, has higher precedence than the logical and .

Concepts in Practice 4.4.8 §4.4 source ↗

Given x = 8 and y = 9, what is the result of the following?
x + 3 * y - 5
  1. a.
    30
  2. b.
    44
  3. c.
    94

Answer

A. 30
The expression is evaluated as x + (3*y) - 5. 8 + (3*9) - 5 = 30.

Concepts in Practice 4.4.9 §4.4 source ↗

Given x = 8 and y = 9, what is the result of the following?
(x+3) * (y-5)
  1. a.
    30
  2. b.
    44
  3. c.
    94

Answer

B. 44
The expressions in parentheses are evaluated first. (8+3) * (9-5) = 11 * 4 = 44.

Concepts in Practice 4.5.1 §4.5 source ↗

Fill in the blank to execute Body 2 when condition_1 is false and condition_2 is true.
if condition_1:
  # Body 1
__ condition_2:
  # Body 2
  1. a.
    if
  2. b.
    elif
  3. c.
    else

Answer

B. elif
elif will evaluate condition_2 if condition_1 is False and execute Body 2 if condition_2 is True .

Concepts in Practice 4.5.2 §4.5 source ↗

Given x = 42 and y = 0, what is the final value of y?
if x > 44:
  y += 2
elif x < 50:
  y += 5
  1. a.
    2
  2. b.
    5
  3. c.
    7

Answer

B. 5
x > 44, 42 > 44, is False , so x < 50 is evaluated. 42 < 50 is True , so y = 0 + 5. Only one of the if and elif branches is taken.

Concepts in Practice 4.5.3 §4.5 source ↗

Which conditions complete the code such that if x is less than 0, Body 1 executes, else if x equals 0, Body 2 executes.
if _________:
  # Body 1
elif _________:
  # Body 2
  1. a.
    x < 0
    x == 0
  2. b.
    x == 0
    x < 0
  3. c.
    x <= 0
    [no condition]

Answer

A. x < 0
x == 0
If x < 0, Body 1 executes. Else if x == 0, Body 2 executes.

Concepts in Practice 4.5.4 §4.5 source ↗

Which of the following is a valid chained decision statement?
  1. a.
    if condition_1:
      # Body 1
    elif condition_2:
      # Body 2
  2. b.
    if condition_1:
      # Body 1
      elif condition_2:
        # Body 2
  3. c.
    elif condition_1:
      # Body 1
    if condition_2:
      # Body 2

Answer

A.
if condition_1:
  # Body 1
elif condition_2:
  # Body 2
The elif must be chained to a preceding statement and at the same indentation level.

Concepts in Practice 4.5.5 §4.5 source ↗

Given attendees = 350, what is the final value of rooms?
rooms = 1
if attendees >= 100:
  rooms += 3
if attendees <= 200:
  rooms += 7
elif attendees <= 400:
  rooms += 14
  1. a.
    4
  2. b.
    15
  3. c.
    18

Answer

C. 18
The if and if-elif statements are not chained. The first if evaluates to True and executes. Then the if-elif is evaluated and the elif branch executes.

Concepts in Practice 4.5.6 §4.5 source ↗

Given hour = 12, what is printed?
if hour < 8:
  print("Too early")
elif hour < 12:
  print("Good morning")
elif hour < 13:
  print("Lunchtime")
elif hour < 17:
  print("Good afternoon")
else:
  print("Too late")
  1. a.
    Good morning
  2. b.
    Lunchtime
  3. c.
    Good afternoon
  4. d.
    Too late

Answer

B. Lunchtime
The third branch executes if hour < 8 and hour < 12 is False and hour < 13 is True .

Concepts in Practice 4.5.7 §4.5 source ↗

Where can an elif statement be added?
_1_
if condition:
  # Body
  _2_
elif condition:
  # Body
_3_
else:
  # Body
_4_
  1. a.
    1
  2. b.
    2
  3. c.
    3
  4. d.
    4

Answer

C. 3
An elif can be chained to another elif to form a longer chained decision.

Concepts in Practice 4.5.8 §4.5 source ↗

Given x = -1 and y = -2, what is the final value of y?
if x < 0 and y < 0:
  y = 10
elif x < 0 and y > 0:
  y = 20
else:
  y = 30
  1. a.
    10
  2. b.
    20
  3. c.
    30

Answer

A. 10
Only one branch is executed in a chained decision statement. -1 < 0 and -2 < 0 is True , so y = 10.

Concepts in Practice 4.5.9 §4.5 source ↗

How could the following statements be rewritten as a chained statement?
if price < 9.99:
  order = 50
if 9.99 <= price < 19.99:
  order = 30
if price >= 19.99:
  order = 10
  1. a.
    if price < 9.99:
      order = 50
    else:
      order = 30
    order = 10
  2. b.
    if price < 9.99:
      order = 50
    elif price < 19.99:
      order = 30
    elif price == 19.99:
      order = 10
  3. c.
    if price < 9.99:
      order = 50
    elif price < 19.99:
      order = 30
    else:
      order = 10

Answer

C.
if price < 9.99:
  order = 50
elif price < 19.99:
  order = 30
else:
  order = 10
The first branch executes if price < 9.99. The second branch executes if price < 19.99 and the first condition fails: that is, price >= 9.99. The third branch executes if the first and second conditions fail: that is, price >= 19.99. Chaining can simplify decision statements.

Concepts in Practice 4.6.1 §4.6 source ↗

Consider the example above. Given leaf_count = 9 and leaf_shape = "teardrop", what is the output?
  1. a.
    Might be poison ivy
  2. b.
    Might be poison oak
  3. c.
    Might be poison sumac
Video · Example: Poisonous plant identification in the book ↗

Answer

C. Might be poison sumac
leaf_count == 3 is False , so the outer else ' s body is executed.

Concepts in Practice 4.6.2 §4.6 source ↗

Given num_dancers = 49, what is printed?
if num_dancers < 0:
  print("Error: num_dancers is negative")
else:
  if num_dancers % 2 == 1:
    print("Error: num_dancers is odd")
  print(num_dancers, "dancers")
  1. a.
    Error: num_dancers is odd
  2. b.
    49 dancers
  3. c.
    Error: num_dancers is odd
    49 dancers

Answer

C. Error: num_dancers is odd
49 dancers
num_dancers is positive, so the else branch is taken. num_dancers is odd, so the error is printed before the program continues after the nested if and prints num_dancers.

Concepts in Practice 4.6.3 §4.6 source ↗

Given x = 256, y = 513, and max = 512, which of the following will execute?
if x == y:
  # Body 1
elif x < y:
  # Body 2
  if y >= max:
    # Body 3
  else:
    # Body 4
else:
  # Body 5
  1. a.
    Body 2
  2. b.
    Body 2, Body 3
  3. c.
    Body 2, Body 5

Answer

B. Body 2, Body 3
256 == 513 is False , and 256 < 513 is True , so the elif branch is taken and Body 2 executes. 513 >= 512 is True , so Body 3 executes.

Concepts in Practice 4.6.4 §4.6 source ↗

Given x =118, y = 300, and max = 512, which of the following will execute?
if x == y:
  # Body 1
elif x < y:
  # Body 2
  if y >= max:
    # Body 3
else:
  # Body 4
else:
  # Body 5
  1. a.
    Body 2
  2. b.
    Body 3
  3. c.
    Error

Answer

C. Error
The first else isn't indented and is treated as the else to the original if . Thus the second else is not connected to an if statement and produces an error.

Concepts in Practice 4.7.1 §4.7 source ↗

What is the conditional expression version of the following if-else statement?
if x%2 == 0:
  response = 'even'
else:
  response = 'odd'
  1. a.
    response = if x%2 == 0 "even" else "odd"
  2. b.
    response = "odd" if x%2 == 0 else "even"
  3. c.
    response = "even" if x%2 == 0 else "odd"

Answer

C. response = "even" if x%2 == 0 else "odd"
The conditional expression will assign response with "even" if x is even. response is assigned with "odd" if x is odd.

Concepts in Practice 4.7.2 §4.7 source ↗

Given x = 100 and offset = 10, what is the value of result?
result = x + offset if x < 100 else x - offset
  1. a.
    90
  2. b.
    100
  3. c.
    110

Answer

A. 90
100 < 100 is False so result is assigned with x - offset = 100 - 10 = 90.

Concepts in Practice 4.7.3 §4.7 source ↗

Which part of the conditional expression is incorrect?
min_num = x if x < y else min_num = y
  1. a.
    min_num = x
  2. b.
    x < y
  3. c.
    min_num = y

Answer

C. min_num = y
min_num cannot be assigned with min_num = y. The correct expression is min_num = x if x < y else y"

Concepts in Practice 4.7.4 §4.7 source ↗

Which of the following is an improved version of the following if-else statement?
if x < 50:
  result = True
else:
  result = False
  1. a.
    result = True if x < 50 else False
  2. b.
    result = x < 50

Answer

B. result = x < 50
Conditional expressions that evaluate to a Boolean are redundant. The Boolean expression should be used instead.

Concepts in Practice 4.7.5 §4.7 source ↗

What are the possible values of total?
total = fee + 10 if hours > 12 else 2
  1. a.
    10, 2
  2. b.
    fee + 10, 2
  3. c.
    fee + 10, fee + 2

Answer

B. fee + 10, 2
The expression is evaluated as (fee + 10) if hours > 12 else 2. So if hours > 12, the result will be fee + 10; otherwise, the result will be 2.

Loading chapter 5…

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.