Chapter 1 Solutions *

Checkpoint 1.1

A program is a(n) ________ that a computer follows to perform a task.

a. user interface

b. operating system

c. set of instructions

d. programming language

Checkpoint 1.2

What term refers to the physical devices, or components, of which a computer is made?

a. hardware

b. software

c. input

d. output

Checkpoint 1.3

What part of the computer actually runs programs?

a. main memory

b. the CPU

c. secondary memory

d. I/O devices

Checkpoint 1.4

What part of the computer serves as a work area to store a program and its data while the program is running?

a. I/O devices

b. secondary memory

c. main memory

d. the CPU

Checkpoint 1.5

What part of the computer holds data for long periods of time, even when there is no power to the computer?

a. I/O devices

b. main memory

c. the CPU

d. secondary storage

Checkpoint 1.6

What part of the computer collects data from people and from other devices?

a. input device

b. output device

c. CPU

d. RAM

Checkpoint 1.7

What part of the computer formats and presents data for people or other devices?

a. input device

b. output device

c. CPU

d. RAM

Checkpoint 1.8

What fundamental set of programs control the internal operations of the computers hardware?

a. file system

b. backup program

c. application interface

d. operating system

Checkpoint 1.9

What do you call a program that performs a specialized task, such as a virus scanner, a file compression program, or a data backup program?

a. utility program

b. application software

c. operating system

d. central processor

Checkpoint 1.10

Word processing programs, spreadsheet programs, email programs, web browsers, and game programs belong to what category of software?

a. utility program

b. application software

c. operating system

d. central processor

Quiz 1.2 (Scored)

Question 1

Monitors, printers, status lights are all examples of _______ devices.

a.  output

b.  input

c.  both input and output

d.  neither input nor output

Question 2

When a program runs on a computer, the part of the computer that carries out the instructions is called the ___

a. RAM

b. GPU

c. ROM

d. CPU

Question 3

When a program runs on a computer, it is stored in _____

a. ROM

b. RAM

c. GPU

d. CPU

Checkpoint 1.11

The amount of memory enough to store a letter of the alphabet or a small number is one bit.

a. True

b. False

Checkpoint 1.12

A tiny “switchthat can be set to either on or off is called a _________.

a. bit

b. byte

c. nibble

d. frame

Checkpoint 1.13

The binary numbering system are all numeric values written as sequences of 0s and 1s.

a. True

b. False

Checkpoint 1.14

ASCII is an encoding scheme that uses a set of __________ to store characters in a computer’s memory.

a. 128 character codes

b. 128 numeric codes

c. 256 numeric codes

d. 256 character codes

Checkpoint 1.15

What encoding scheme is extensive enough to represent the characters of many of the languages in the world?

a. Universal

c. ASCII

d. Unicode

Checkpoint 1.16

Any data that is stored in binary is known as analog data.

a. True

b. False

Quiz 1.3 (Scored)

Question 1

A bit is

a. an alternative term for byte.

b. a small amount of data.

c. an electronic device used in computers.

d. a binary digit, like 0 or 1.

Question 2

A byte is made up of ___________ bits.

2

8

16

32

Question 3

A binary digit

a. requires one byte of storage.

b. is none of the above.

c. is zero or one.

d. is either positive or negative.

e. is 2.

Checkpoint 1.17

A CPU understands instructions that are written only in _________.

a. object code

b. program code

c. hex language

d. machine language

Checkpoint 1.18

A program has to be copied into what type of memory each time the CPU executes it?

a. RAM

b. ROM

c. BUS

d. DISC

Checkpoint 1.19

When a CPU executes the instructions in a program, it is engaged in what process?

a. code switching

b. fetch-decode-execute cycle

c. in-out processing

d. device communication

Checkpoint 1.20

What format uses short words that are known as mnemonics?

b. binary format

c. assembly language

d. high-level language

Checkpoint 1.21

A high-level programming language allows you to create powerful and complex programs without knowing how the CPU works.

a. True

b. False

Checkpoint 1.22

Each language has a set of rules that must be strictly followed when writing a program. What is this set of rules called?

a. grammar

b. protocols

c. semantics

d. syntax

Checkpoint 1.23

What type of program translates a high-level language program into a separate machine language program?

a. compiler

b. router

c. analyzer

d. processor

Checkpoint 1.24

A mistake that is usually caused by a misspelled key word, a missing punctuation character, or the incorrect use of an operator is called a logical error.

a. True

b. False

Quiz 1.4 (Socred)

Question 1

Which statement is not true:

a. Machine languages can be used to express algorithms.

b. Machine language is produced by compilers.

c. Machine language consists of zeros and ones.

d. Machine languages can be used to write programs that can run on any machine.

Question 2

A(n) _____________ is a program that both translates and executes the instructions in a program.

a. compiler

c. editor

d. interpreter

Question 3

A compiler

a. translates executable code to machine code

b. maintains a collection of programs

c. tests a program's logic

d. translates source code into executable machine language code

Question 4

The code that a programmer writes is called _____ code.

a. exe

b. source

c. machine

d. object

Question 5

The rules that govern the correct order and usage of the elements of a language are called the ______ of the language.

a. syntax

b. lexicon

c. form

d. method

Question 6

Words that have a special meaning in a programming language are called _____.

a. format words

b. special words

c. programming words

d. key words

Live Code Example 1.1

Type the following statement in the interface shown below.

print('Python programming is fun!')

Live Code Example 1.2

Type the following statement in the interface shown below.

print('To be or not to be')

print('To be or not to be')

Checkpoint 1.25

The program that can read Python programming statements and execute them is called a Python _________.

a. analyzer

b. router

c. interpreter

d. processor

Checkpoint 1.26

When programming in _________ mode, the interpreter waits for you to type a Python statement on the keyboard and then executes it.

a. user

b. batch

c. interactive

d. script

Checkpoint 1.27

When programming in _________ mode, the interpreter reads the contents of a file that contains Python statements and executes each statement as it reads it.

a. user

b. batch

c. interactive

d. script

Checkpoint 1.28

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Chapter 2 Solutions

Checkpoint 2.1

Any person, group, or organization that is asking you to write a program is known as a _________.

a. user interface

c. domain controller

d. programmer’s customer

Checkpoint 2.2

A single function that the program must perform in order to satisfy a customer is known as a protocol.

a. True

b. False

Checkpoint 2.3

A set of well-defined logical steps that must be taken to perform a task is called a(n) _________.

a. policy

b. algorithm

c. matrix

d. solution

Checkpoint 2.4

What is an informal language that is used to create “mock-ups” of programs?

a. script

b. policy

c. pseudocode

d. interface

Checkpoint 2.5

A diagram that graphically depicts the steps that take place in a program is called a factorization.

a. True

b. False

Checkpoint 2.6

In a flowchart, _________ are terminal symbols.

a. ovals

b. parallelograms

c. rectangles

d. triangles

Checkpoint 2.7

In a flowchart, _________ are either output or input.

a. ovals

b. parallelograms

c. rectangles

d. triangles

Checkpoint 2.8

In a flowchart, _________ are processing symbols.

a. ovals

b. parallelograms

c. rectangles

d. triangles

Checkpoint 2.9

Any data that the program receives while it is running is known as _________.

a. translation

b. compilation

c. output

d. input

Checkpoint 2.10

The results of a(n) ________ are sent out of a program as output.

a. operating system

b. CPU state

c. executing process

d. memory module

Checkpoint 2.11

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Checkpoint 2.12

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Checkpoint 2.13

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Quiz 2.3 (Scored)

Question 1

Which of the following statements displays this output:

one two three

a.       print('one two three')

b.      print("one two three")

c.       print('''one two three''')

d.      All of the above

d)        All of the above

Checkpoint 2.14

Comments are intended for any person reading a program’s code, not the computer.

a. True

b. False

Checkpoint 2.15

What component of a program references a value in the computer's memory?

a. syntax

c. variable

d. package

Checkpoint 2.16

What is an illegal variable name in Python?

a. x

b. july_2019

c. theSalesFigureForFiscalYear

d. r&d

Checkpoint 2.17

The variable name Sales is considered to be the same as sales in Python.

a. True

b. False

Checkpoint 2.18

The following assignment statement is valid:

72 = amount

a. True

b. False

Checkpoint 2.19

What will the following code display?

val = 99

print('The value is', 'val')

a. The value is ‘99’

b. The value is ‘val’

c. The value is 99

d. The value is val

Checkpoint 2.20

After the statements below execute, what is the Python data type of the values referenced by each variable?

value1 = 99

value2 = 7

a. int

b. float

c. char

d. str

Checkpoint 2.21

After the statements below execute, what is the Python data type of the values referenced by each variable?

value1 = 45.9

value2 = 7.0

a. int

b. float

c. char

d. str

Checkpoint 2.22

After the statements below execute, what is the Python data type of the value referenced by the variable?

value1 = 'abc'

a. int

b. float

c. char

d. str

Checkpoint 2.23

What will be displayed by the following program?

my_value = 99

my_value = 0

print(my_value)

a. 0

b. 99

c. my_value

d. my_value = 0

Quiz 2.5 (Scored)

Question 1

Assign 7 to a variable named seven.

seven = 7

Question 2

Assign 8 to a variable named eight.

eight = 8

Question 3

Write two assignment statements. The first should  assign 3.5 to a variable named length, and the second should assign 1.55 to a variable named width.

length = 3.5
width = 1.55

Question 4

Which of the following names in a program is equivalent to the name int?

Neither

Question 5

Which of the following is NOT a legal identifier?

7thheaven

Question 6

Which is the best identifier for a variable to represent the amount of money your boss pays you each month?

monthlyPay

Question 7

Given a variable count, which contains an integer value, write a statement that writes the value of count to standard output.

print(count)

Checkpoint 2.24

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Checkpoint 2.25

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Quiz 2.6 (Scored)

Question 1

Write code that performs the following input operations:

·         Read an int from the keyboard and assign it to a variable named k. (Do not print a prompt. Use the input() function without a prompt-string to read the input.)

·         Read a float from the keyboard and assign it to a variable named d. (Do not print a prompt. Use the input() function without a prompt-stringto read the input.)

·         Read a string from the keyboard and assign it to a variable named s. (Do not print a prompt. Use the input() function without a prompt-string to read the input.)

After you have performed the input, on one line, print these variables in reverse order (s followed by d, followed by k) with exactly one space in between each. On a second line, print them in the original order (k followed by d, followed by s) with one space in between them.

k = int(input())
d = float(input())
s = input()
print(s, d, k)
print(k, d, s)

Checkpoint 2.26

What is the value of the following expression?

6 + 3 * 5

a. 14

b. 21

c. 45

d. 90

Checkpoint 2.27

What is the value of the following expression?

12 / 2 – 4

a.  -6

b. 2

c. 4

d. 6

Checkpoint 2.28

What is the value of the following expression?

9 + 14 * 2 – 6

a. 29

b. 31

c. 33

d. 40

Checkpoint 2.29

What is the value of the following expression?

(6 + 2) * 3

a. 12

b. 20

c. 24

d. 36

Checkpoint 2.30

What is the value of the following expression?

14 / (11 − 4)

a. -3

b. -2

c. 10/11

d. 2

Checkpoint 2.31

What is the value of the following expression?

9 + 12 * (8 − 3)

a. 26

b. 69

c. 105

d. 165

Checkpoint 2.32

What value will be assigned to result after the following statement executes?

result = 9 // 2

a. 4

b. 4.5

c. 5

Checkpoint 2.33

What value will be assigned to result after the following statement executes?

result = 9 % 2

a. 1

b. 2

c. 4

d. 5

Checkpoint 2.34

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Quiz 2.7 (Scored)

Question 1

A wall has been built with two pieces of sheetrock, a smaller one and a larger one. The length of the smaller one is stored in the variable small. Similarly, the length of the larger one is stored in the variable large. Write a single expression whose value is the length of this wall.

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

large + small

Question 2

Write an expression that computes the difference of the variables ending_time and starting_time.

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

ending_time - starting_time

Question 3

Given the variables full_admission_price and discount_amount (already defined), write a math expression that gives the price of a discount admission.

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

Question 4

Given the variable price_per_case, write a math expression that gives the price of a dozen cases.

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

price_per_case * 12

Question 5

Given the variables cost_of_bus_rental and max_bus_riders, write a math expression that gives the cost per rider (assuming the bus is full).

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

cost_of_bus_rental / max_bus_riders

Question 6

Write a math expression that computes the remainder of the variable principal when divided by the variable divisor. (Assume that each is refers to an int.)

Note: Simply write the math expression. Do not assign the value of the expression to a variable.

principal % divisor

Question 7

Write a statement that calculates the average of 12 and 40, and assigns the result to a variable named avg.

avg = (12 + 40) / 2

Question 8

Calculate the average of the variables ab, and c, and assign the result to a variable named avg. Assume that the variables ab, and c have already been assigned a value, but do not assume that the values are all floating-point. Make sure the value that you assign to avg is a floating-point value.

avg = float(a+b+c)/3

Live Code Example 2.1
Complete the code below to produce this output:

Enter the value of num_1: 15
Enter the value of num_2: 4
num_1 + num_2 = 19
num_1 - num_2 = 11
num_1 * num_2 = 60
num_1 // num_2 = 3

num_1 = int(input('Enter the value of num_1: '))

num_2 = int(input('Enter the value of num_2: '))

num_1 = int(input('Enter the value of num_1: '))

num_2 = int(input('Enter the value of num_2: '))

print('num_1 + num_2 =', num_1 + num_2)

print('num_1 - num_2 =', num_1 - num_2)

print('num_1 * num_2 =', num_1 * num_2)

print('num_1 // num_2 =', num_1 // num_2)

#Change the values of the variables and the operands and check your results.

Live Code Example 2.2
The given code should display the result of 5 * 2.6666666 as an integer, but it is not! Correct the error.

rounded_int = 5 * 2.6666666

print rounded_int

rounded_int = int (5 * 2.6666666)

print (rounded_int)

Live Code Example 2.3
You are on a road trip and stopped for gas. You decide to calculate your average miles per gallon (MPG) since your last gas stop. Type in the code below and fill in the missing line under the last comment to display the MPG.

start = float(input('Enter your starting mileage: '))

end = float(input('Enter your ending mileage: '))

gallons = float(input('Enter the gallons used: '))

# Calculate the average miles per gallon.

average = (end - start) / gallons

# Display the average to two decimal places.

start = float(input('Enter your starting mileage: '))

end = float(input('Enter your ending mileage: '))

gallons = float(input('Enter the gallons used: '))

average = (end - start) / gallons

print('The miles per gallon is', format(average, '.2f'))

Checkpoint 2.35

If you do not want the print function to start a new line of output when it finishes displaying its output, you can pass the special argument ________ to the function.

a. sep=

b. ‘new’

c. ‘\ln’

d. end = ' '

Checkpoint 2.36

You can change the character that is automatically displayed between multiple items that are passed to the print function by passing the ________ argument with the desired character.

a. end = ' '

b. ‘new’

c. ‘\c’

d. sep=

Checkpoint 2.37

The newline escape character is ________.

a. ‘n’

b. ‘nl’

c. '\n'

d. ‘\nl’

Checkpoint 2.38

The + operator, when used with two strings, joins them together as one string.

a. True

b. False

Checkpoint 2.39

What will the statement print(format(65.4321, '.2f')) display?

a. 65.4321

b. 6543.21

c. 65.00

d. 65.43

Checkpoint 2.40

What will the statement print(format(987654.129, ',.2f')) display?

a. 987,654.00

b. 987654.12

c. 987,654.13

d. 987654.13

Checkpoint 2.41

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Quiz 2.8 (Scored)

Question 1

Given two variables iVal and fVal, containing respectively an integer and a float value, write a statement that writes both of their values to standard output in the following format: i=iVal f=fVal.

For example, if iVal is assigned 5 and fVal is assigned 2.4, the statement should print the following:

i=5 f=2.4

print("i=" + str(iVal), "f=" + str(fVal))

Question 2

The character escape sequence to force the cursor to go to the next line is ____.
The character escape sequence to force the cursor to advance forward to the next tab setting is ____.
The character escape sequence to represent a single quote is ____.
The character escape sequence to represent a double quote is ____.
The character escape sequence to represent a backslash is ____.

\n
\t
\'
\"
\\

Checkpoint 2.42

Named constants make programs more self-explanatory.

a. True

b. False

Checkpoint 2.43

Named constants make widespread changes more difficult to make in a program.

a. True

b. False

Checkpoint 2.44

Named constants help to prevent the typographical errors that are common when using magic numbers.

a. True

b. False

Checkpoint 2.45

Which Python statement best defines a named constant for a 10 percent discount?

a. PERCENTAGE = 10%

b. discount percent = 10

c. dp = 0.100

d. DISCOUNT_PERCENTAGE = 0.1

Checkpoint 2.46

Which command will move a turtle forward?

a. turtle.walk

b. turtle.forward

c. turtle.move

d. turtle.on

Checkpoint 2.47

Which command will turn a turtle right by 45 degrees?

a.  turtle.right = 45

b. turtle.move(45, right)

c. turtle.right(45)

d. turtle.move(right, 45)

Checkpoint 2.48

To move a turtle to a new location without drawing a line, first use the _________ command to raise the turtle’s pen.

a. turtle.penup()

b. turtle.liftpen()

c. turtle.locate()

d. turtle.moveup()

Checkpoint 2.49

What command would you use to display a turtle’s current heading?

a. turtle.current()

c. turtle.locate()

d. turtle.coordinates (x, y)

Checkpoint 2.50

What command would you use to display the coordinates of the turtle’s current position?

a. turtle.coor(x, y)

b. turtle.pos()

c. turtle.locate(x, y)

d. turtle.display()

Checkpoint 2.51

Which of the following commands will make the animation speed faster?

a. turtle.speed(on)

b. turtle.speed(1)

c. turtle.speed(true)

d. turtle.speed(10)

Checkpoint 2.52

To fill a turtle shape with a color, use the ________ command before drawing the shape, then use the ________ command after the shape is drawn.

a. turtle.begin_fill(); turtle.end_fill()

b. turtle.start_fill(); turtle.finish_fill()

c. turtle.fill(start) ; turtle.fill(finish)

d. turtle.fill(begin) ; turtle.fill(end)

Checkpoint 2.53

What command would you use to display text in the turtle’s graphics window?

a. turtle.draw()

b. turtle.window()

c. turtle.write()

d. turtle.display()

Chapter 2 Programming Project 1 (Scored)

A cookie recipe calls for the following ingredients:
• 1.5 cups of sugar
• 1 cup of butter
• 2.75 cups of flour
The recipe produces 48 cookies with this amount of ingredients. Write a program that asks the user how many cookies they want to make and then displays the number of cups of each ingredient needed for the specified number of cookies.

When the program asks the user for the number of cookies, it should display the following string as a prompt:

When the program displays the number of cups of ingredients, it should display a message in the following format:

You need x cups of sugar, y cups of butter, and z cups of flour.

Where x is the number of cups of sugar, y is the number of cups of butter, and z is the number of cups of flour. Don’t worry about formatting the numbers in the output.

The following sample run shows an example of the program's output. The user's input is shown in bold.

Sample Run

You need 1.5 cups of sugar, 1.0 cups of butter, and 2.75 cups of flour.

cups_sugar = 1.5 * num_cookies / 48

cups_flour = 2.75 * num_cookies / 48

print('You need', cups_sugar, 'cups of sugar,', cups_butter, 'cups of butter, and', cups_flour, 'cups of flour.')

Chapter 2 Programming Project 2 (Scored)

Several litters of new pups were recently born in the otter habitat at the local zoo. Write a program that calculates the percentage of male and female otters currently in the habitat.

The program should ask the user for the number of males and the number of females using two separate input statements. When the program asks for the number of males, it should display the following string as a prompt:

'Enter number of males:'

When the program asks for the number of females, it should display the following string as a prompt:

'Enter number of females:'

The program should display the percentage of males and females (round to the nearest whole number) in the following format:

Percent males: 35%
Percent females: 65%

Use string formatting in your output.

The following sample run shows an example of the program's output. The user's input is shown in bold.

﻿Sample Run

﻿Enter number of males:75
Enter number of females:25

Percent males: 75%

Percent females: 25%

males = int(input('Enter number of males:'))

females = int(input('Enter number of females:'))

total = males + females

percentMale = males / total

percentFemale = females / total

print('Percent males:', format(percentMale, '.0%'))

print('Percent females:', format(percentFemale, '.0%'))

Chapter 3 Solutions

Checkpoint 3.1

A logical design that controls the order in which a set of statements execute is known as a _________.

a. decision structure

b. control structure

c. process diagram

d. flow diagram

Checkpoint 3.2

A program structure that can execute a set of statements only under certain circumstances is called a _________.

a. decision structure

b. control structure

c. process diagram

d. flow diagram

Checkpoint 3.3

In a single alternative decision structure, if the condition that is being tested is __________, the program takes the alternative path.

a.      zero

b.      true

c.       incorrect

d.     false

Checkpoint 3.4

A Boolean expression can be evaluated as either true, false, or null.

a. True

b. False

Checkpoint 3.5

Relational operators can be used to determine whether one value is greater than, less than, greater than

or equal to, less than or equal to, equal to, or not equal to another value.

a. True

b. False

Checkpoint 3.6

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Checkpoint 3.7

Every embedded activity in Revel is designed to support and deepen student learning. However, this Checkpoint item did not deliver the intended learning experience and has therefore been removed. A revised version of this Checkpoint may appear in the next product release.

Quiz 3.1 (Scored)

Question 1

Write a Boolean expression that is True if the value of x is equal to zero.

x == 0

Question 2

Write a Boolean expression that is True if the variables profits and losses are exactly equal.

profits == losses

Question 3

Write an expression that evaluates to True if the value of index is greater than the value of last_index.

index > last_index

Question 4

Write an expression that evaluates to True if x is greater than or equal to y.

x >= y

Question 5

Write a Boolean expression that is True if x is greater than y.

x > y

Question 6

Assume the variables x and y have each been assigned an int. Write a fragment of code that assigns the greater of these two variables to another variable named max.

max = x
if y > max:
max = y

Question 7

Assume the variables x, y, and z have each been assigned an integer value. Write a fragment of code that assigns the least of these three variables to another variable named min.

min = x
if  y < min:
min = y
if  z < min:
min = z

Question 8

Write an if statement that decreases the value of shelf_life by 4 if the value of outside_temperature is greater than 90.

if outside_temperature > 90:

shelf_life -= 4

Checkpoint 3.8

A ________ decision structure has two possible paths of execution; one path is taken if a condition is true, and the other path is taken if the condition is false.

a. primary-secondary

b. looping

c. dual alternative

d. phasing

Checkpoint 3.9

What statement do you use in Python to write a dual alternative decision structure?

a. do-until

b. if-else

c.  for

d. while

Live Code Example 3.1

The program below calculates the price of an order of bagels based on the number of bagels purchased. Up to 12 bagels are $1.50, and any bagels purchased in addition are$0.75 cents each.

num_bagels = float(input('How many bagels would you like to order? '))

# Write the if condition

price = num_bagels * 1.5

else:

# Write a statement to calculate the cost for

# an order of more than 12 bagels

print('Your total is $', format(price, ',.2f'), sep='') Answer: num_bagels = float(input('How many bagels would you like to order? ')) if num_bagels <= 12: price = num_bagels * 1.5 else: price = (12 * 1.5) + ((num_bagels - 12) * .75) print('Your total is$', format(price, ',.2f'), sep='')

Live Code Example 3.2

Correct the error in the code below.

Expected Output:
Enter the length of the cube's side: 5
The area of this cube is 125

side = int(input('Enter the length of the cube\'s side: '))

if side >= 0:

print('The volume of this cube is', side * 3 )

else:

side = int(input('Enter the length of the cube\'s side: '))

if side >= 0:

print('The volume of this cube is', side ** 3 )

else:

Checkpoint 3.10

When you write an if-else statement, the statements that appear after the else clause execute _________.

a. until the condition is false

b. when the condition is false

c. when the condition is true

d. until the condition is  true

Quiz 3.2 (Scored)

Question 1

Write an if-else statement that compares the age variable with the value 65. If age is greater than or equal to 65, add 1 to senior_citizens. Otherwise, add 1 to non_seniors.

if age >= 65:
senior_citizens += 1
else:
non_seniors += 1

Question 2

Write an if-else statement that assigns True to fever if temperature is greater than 98.6; otherwise it assigns False to fever.

if temperature > 98.6:
fever = True
else:
fever = False

Checkpoint 3.11

What would the following code display?

if 'z' < 'a':

print('z is less than a.')

else:

print('z is not less than a.')

a. z is less than a.

b. z is not less than a.

c. “”

Checkpoint 3.12

What would the following code display?

s1 = 'New York'

s2 = 'Boston'

if s1 > s2:

print(s2)

print(s1)

else:

print(s1)

print(s2)

a.

Boston

New York

b.

New York

Boston

c.

New York

d.

Boston

Live Code Example 3.3

The code below validates the password ThisI$MyPassw0rd. Provide the if statement. password = input('Enter a password: ') # Supply an if statement here to check for the password 'ThisI$MyPassw0rd'

print('You\'re in.')

else:

print('You\'re in.')

else:

Live Code Example 3.4

Indent the
if statements correctly.

num_hours = int(input('How many hours are you taking this semester? '))

num_majors = int(input('How many of these courses are in your major? '))

if num_hours >= 12:

if num_majors >= 3:

else:

print('Double check the requirements for your major.')

else:

num_hours = int(input('How many hours are you taking this semester? '))

num_majors = int(input('How many of these courses are in your major? '))

if num_hours >= 12:

# outer if block

if num_majors >= 3:

# inner if block

else:

print('Double check the requirements for your major.')

else:

Quiz 3.3 (Scored)

Question 1

Write a Boolean expression that is True if s references the string "end".

s == "end"

Question 2

Assume both the variables s1 and s2 have been assigned strings. Write an expression that evaluates to True if s1 is greater than s2.

s1 > s2

Question 3

Write a Boolean expression that is True if the variable s does not reference the string "end".

s != "end"

Live Code Example 3.5

The code below asks the user how many steps they have walked in a day. Feedback is given based on the number of steps, which are defined as constants.

# Constants for step levels

A_STEPS = 10000

B_STEPS = 8000

C_STEPS = 6000

D_STEPS = 4000

numsteps = int(input('How many steps did you walk today? '))

if numsteps >= A_STEPS:

print('Great job!')

# Fill in the missing code here

print('Good work! Just a few more left to go.')

elif numsteps >= C_STEPS:

print('Nice! More than halfway there!')

elif numsteps >= D_STEPS:

print('Good start! Keep active the rest of the day!')

# Fill in the missing code here

print('No worries. Still time to get some steps in!')

# Constants for step levels

A_STEPS = 10000

B_STEPS = 8000

C_STEPS = 6000

D_STEPS = 4000

numsteps = int(input('How many steps did you walk today? '))

if numsteps >= A_STEPS:

print('Great job!')

elif numsteps >= B_STEPS:

print('Good work! Just a few more left to go.')

elif numsteps >= C_STEPS:

print('Nice! More than halfway there!')

elif numsteps >= D_STEPS:

print('Good start! Keep active the rest of the day!')

else:

print('No worries. Still time to get some steps in!')

Live Code Example 3.6

You have 1000 possible points in a class and are trying to decide whether to take an optional final. The code below contains an error. Fill in the commented line with the missing code to display the expected output. Running points that are above or equal to 800 points display the “You're doing fine” message.

running_total = int(input('How many points do you have in this course?  '))

if running_total >= 900:

print('Congratulations! ')

print('There is no need to take the final ')

elif running_total >= 850:

print('Good work! ')

print('You might want to take the final. ')

# Fill in the missing statement here

print('You\'re doing fine. ')

print('But you might consider taking the final. ')

else:

print('We recommend taking the final. ')

print('Find a study partner ... and good luck! ')

running_total = int(input('How many points do you have in this course?  '))

if running_total >= 900:

print('Congratulations! ')

print('There is no need to take the final ')

elif running_total >= 850:

print('Good work! ')

print('You might want to take the final. ')

elif running_total >= 800:

print('You\'re doing fine. ')

print('But you might consider taking the final. ')

else:

print('We recommend taking the final. ')

print('Find a study partner ... and good luck! ')

Live Code Example 3.7

# Fill in the missing statement here

print ('Your user password is still set to the default. Please change it to enter the forum.')

else:

print ('Welcome to the community forum.')

print ('User password is still set to the default. Please change it to enter the forum.')

else:

print ('Welcome to the community forum.')

Quiz 3.4 (Scored)

Question 1

Assume that ph has been assigned a float. Write an if-else-if statement that

compares ph to 7.0 and makes the following assignments (respectively) to the variables neutralbase, and acid:

·         0,0,1 if ph is less than 7

·         0,1,0 if ph is greater than 7

·         1,0,0 if ph is equal to 7

if ph < 7:

neutral = 0

base = 0

acid = 1

elif ph > 7:

neutral = 0

base = 1

acid = 0

else:

neutral = 1

base = 0

acid = 0

Checkpoint 3.13

A compound Boolean expression is created by using a concatenation operator to combine two

Boolean subexpressions.

a. True

b. False

Checkpoint 3.14

The following logical expression evaluates to either true (T) or false (F). Choose the correct result.

True and False

a. True

b. False

Checkpoint 3.15

The following logical expression evaluates to either true (T) or false (F). Choose the correct result.

True and True

a. True

b. False