ction
,SolutioncxandcxAnswercxGuidecxForcxAllcxChapters:cxLambert,cxFundamentalscxofcxPython:cxFirstcxPrograms,cx3e,cxCY24,cx9780357881019;cxChaptercx1,cxIntrodu
ction
TABLE OF CONTENTS CX CX
Exercise Solutions ....................................................................................................................................... 1
cx
Exercise 1.1 ............................................................................................................................................... 1
cx
Exercise 1.2 ............................................................................................................................................... 2
cx
Exercise 1.3 ............................................................................................................................................... 3
cx
Review Questions Answers......................................................................................................................... 4
cx cx
Programming Exercises Solutions ............................................................................................................. 8
cx cx
Debugging Exercises Solutions .................................................................................................................. 8
cx cx
EXERCISE SOLUTIONS CX
EXERCISE 1.1 CX
1. List three common types of computing agents.
cx cx cx cx cx cx
Solution:
Human beings, desktop computers, cell phones
cx cx cx cx cx
2. Write an algorithm that describes the second part of the process of making change (counting out the c
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
oins and bills).
cx cx
Solution:
There are various ways to do this, but here is one:
cx cx cx cx cx cx cx cx cx cx
Repeat
Select the largest unit of money that is less than or equal to the remaining cha
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
nge Subtract this unit from the remaining change
cx cx cx cx cx cx cx
Until the remaining change is 0
cx cx cx cx cx
The collection of units selected represent the change
cx cx cx cx cx cx cx
3. Write an algorithm that describes a common task, such as baking a cake.
cx cx cx cx cx cx cx cx cx cx cx cx
,SolutioncxandcxAnswercxGuidecxForcxAllcxChapters:cxLambert,cxFundamentalscxofcxPython:cxFirstcxPrograms,cx3e,cxCY24,cx9780357881019;cxChaptercx1,cxIntrodu
ction
Solution:
There are various ways to do this, but here is one:
cx cx cx cx cx cx cx cx cx cx
Preheat an oven to 375 degrees cx cx cx cx cx
Add 1 cup of water and 1 egg to a mixing bowl
cx cx cx cx cx cx cx cx cx cx cx
Beat the liquid mixture in the bowl until the ingredients are blended
cx cx cx cx cx cx cx cx cx cx cx c
Add the contents of a boxed cake mix to the mixing bowl
x cx cx cx cx cx cx cx cx cx cx cx
Beat the mixture in the bowl until the ingredients are blended
cx cx cx cx cx cx cx cx cx cx
Pour the contents of the mixing bowl into a lightly greased cake pa
cx cx cx cx cx cx cx cx cx cx cx cx
n Bake the cake in the oven for 45 minutes
cx cx cx cx cx cx cx cx cx
4. Describe an instruction that is not well defined and thus could not be included as a step in an algori
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
thm. Give an example of such an instruction.
cx cx cx cx cx cx cx
Solution:
Attempting to divide a number by 0 cx cx cx cx cx cx
5. In what sense is a laptop computer a general-purpose problem-solving machine?
cx cx cx cx cx cx cx cx cx cx
Solution:
A laptop computer is a general-purpose problem-
cx cx cx cx cx cx
solving machine because it is programmable and can solve any problem for which there is an algorith
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
m.
6. List four devices that use computers and describe the information that they process. (Hint: Think of
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
the inputs and outputs of the devices.)
cx cx cx cx cx cx cx
Solution:
Digital camera—images, music player—sound, cell phone—text, ATM—numbers
cx cx cx cx cx cx
EXERCISE 1.2 CX
1. List two examples of input devices and two examples of output devices.
cx cx cx cx cx cx cx cx cx cx cx
Solution:
Input devices—keyboard and mouse, output devices—monitor and speakers
cx cx cx cx cx cx cx
2. What does the central processing unit (CPU) do?
cx cx cx cx cx cx cx
Solution:
The CPU fetches, decodes, and executes instructions.
cx cx cx cx cx cx
3. How is information represented in hardware memory?
cx cx cx cx cx cx
Solution:
Information is represented using binary notation, which in hardware is a pattern of voltage levels.
cx cx cx cx cx cx cx cx cx cx cx cx cx cx
, SolutioncxandcxAnswercxGuidecxForcxAllcxChapters:cxLambert,cxFundamentalscxofcxPython:cxFirstcxPrograms,cx3e,cxCY24,cx9780357881019;cxChaptercx1,cxIntrodu
ction
4. What is the difference between a terminal-based interface and a graphical user interface?
cx cx cx cx cx cx cx cx cx cx cx cx
Solution:
A terminal-
cx
based interface supports only the input and output of text with a keyboard and monitor. A graphical u
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
ser interface supports the output of images and the manipulation of them with a pointing device, the mo
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
use.
5. What role do translators play in the programming process?
cx cx cx cx cx cx cx cx
Solution:
A translator converts a program written in a high-
cx cx cx cx cx cx cx cx
level language (human readable and writable) to an equivalent program in a low-
cx cx cx cx cx cx cx cx cx cx cx cx
level language (machine readable and executable).
cx cx cx cx cx
EXERCISE 1.3 CX
1. Describe what happens when the programmer enters the string "Greetings!" in the Python shell.
cx cx cx cx cx cx cx cx cx cx cx cx cx
Solution:
Python reads the string "Greetings!", evaluates it, and displays this string (including single quot
cx cx cx cx cx cx cx cx cx cx cx cx cx
es) in the shell.
cx cx cx
2. Write a line of code that prompts the user for their name and saves the user’s input in a variable called
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
name.
Solution:
name= input("Enter your name: ")
x
c cx cx cx cx
3. What is a Python script?
cx cx cx cx
Solution:
A Python script is a complete Python program that can be run from a computer’s operating system.
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
4. Explain what goes on behind the scenes when your computer runs a Python program.
cx cx cx cx cx cx cx cx cx cx cx cx cx
Solution:
If the program has not already been translated, Python’s compiler translates it to byte code. The Pytho
cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx cx
n virtual machine then executes this code.
cx cx cx cx cx cx