Ch4 Python
Ch4 Python
JAAMACADDA SIMAD
Faculty:
Program:
Class:
ID No. :
Phone No. :
Subject : Pearson Starting Out with Python 5rd Tony Gaddis (2021)
A2) A loop that uses a true/false condition to control the number of times that it repeats
Q5) Does the while loop test its condition before or after it performs an iteration?
A5) Before
Q6) How many times will 'Hello World' be printed in the following program?
count = 10
print('Hello World')
A6) None. The condition count < 0 will be false to begin with.
A7) A loop that has no way of stopping and repeats until the program is interrupted.
Q8) Rewrite the following code so it calls the range function instead of using the list
[0, 1, 2, 3, 4, 5]:
print(number)
A9) 0
1
October 14, 2021
2
print(number)
A10) 2
print(number)
A11) 0
100
200
300
400
500
print(number)
A12) 10
9
October 14, 2021
8
Q14) Should an accumulator be initialized to any specific value? Why or why not?
A14) Yes, it should be initialized with the value 0. This is because values are added to the accumulator by
a loop. If the accumulator does not start at the value 0, it will not contain the correct total of the
numbers that were added to it when the loop ends.
total = 0
print(total)
A15) 15
number 1 = 10
number 2 = 5
print(number1)
print(number2)
A16) 15
a) quantity = quantity + 1
b) days_left = days_left − 5
c) price = price * 10
d) price = price / 2
October 14, 2021
A17) a) quantity += 1
b) days_left −= 5
c) price *= 10
d) price /= 2
A18) A sentinel is a special value that marks the end of a list of items.
Q19) Why should you take care to choose a distinctive value as a sentinel?
A19) A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.
Q20) What does the phrase “garbage in, garbage out” mean?
A20) It means that if bad data (garbage) is provided as input to a program, the program will produce bad
data (garbage) as output.
A21) When input is given to a program, it should be inspected before it is processed. If the input is
invalid, then it should be discarded and the user should be prompted to enter the correct data.
Q22) Describe the steps that are generally taken when an input validation loop is used to validate
data.
A22) The input is read, then a pretest loop is executed. If the input data is invalid, the body of the loop
executes. In the body of the loop, an error message is displayed so the user will know that the input was
invalid, and then the input read again. The loop repeats as long as the input is invalid.
A23) It is the input operation that takes place just before an input validation loop. The purpose of the
priming read is to get the first input value.
Q24) If the input that is read by the priming read is valid, how many times will the input validation
loop iterate
A24) None
1. A __________ -controlled loop uses a true/false condition to control the number of times that it
repeats.
5. A(n) __________ loop has no way of ending and repeats until the program is interrupted.
8. A(n) __________ is a special value that signals when there are no more items from a list of items to
be processed. This value cannot be mistaken as an item from the list.
a. great input, great output b. garbage in, garbage out c. GIGahertz Output d. GIGabyte Operation
10. The integrity of a program’s output is only as good as the integrity of the program’s .
11. The input operation that appears just before a validation loop is known as the .
5. In a nested loop, the inner loop goes through all of its iterations for every single itera- tion of
True
the outer loop.
True 6. To calculate the total number of iterations of a nested loop, add the number of itera- tions of
all the loops.
7. The process of input validation works as follows: when the user of a program enters invalid
True
data, the program should ask the user “Are you sure you meant to enter that?” If the user
answers “yes,” the program should accept the data.
Short Answer
A1) A condition-controlled loop uses a true/false condition to control the number of times that it
repeats.
Q3) What is an infinite loop? Write the code for an infinite loop.
A3) An infinite loop continues to repeat until the program is interrupted. Infinite loops usually occur
when the programmer forgets to write code inside the loop that make test condition false. Example:
x = 99
While 》 0
Display x
End While
A4) If the accumulator starts with any value other than 0, it will not contain the correct total when the
loop finishes.
A5) You can write a loop that processes a list of data items when you do not know the number of data
items in the list, and (1) you do not want to ask the user if there are more items at the end of each loop
iteration, and (2) you do now want to require the user to know the number of items in the list in
advance.
A6) A sentinel value must be unique enough that it will not be mistaken as a regular value in the list.
Q7) What does the phrase “garbage in, garbage out” mean?
A7) It means that if bad data (garbage) is provided as input to a program, the program will produce bad
data (garbage) as output.
A8) When input is given to a program, it should be inspected before it processed. If the input is invalid,
then it should be discarded and the user should be prompted to enter the correct data.