0% found this document useful (0 votes)
32 views

python_chapter_four[1]

Chapter Four of the Python document covers repetition structures, including condition-controlled and count-controlled loops, as well as concepts like loop iterations, infinite loops, accumulators, and input validation. It provides examples and explanations of how these structures work, their purposes, and the importance of proper input handling. Additionally, it includes multiple-choice and true/false questions to reinforce understanding of the material.

Uploaded by

danteyda17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

python_chapter_four[1]

Chapter Four of the Python document covers repetition structures, including condition-controlled and count-controlled loops, as well as concepts like loop iterations, infinite loops, accumulators, and input validation. It provides examples and explanations of how these structures work, their purposes, and the importance of proper input handling. Additionally, it includes multiple-choice and true/false questions to reinforce understanding of the material.

Uploaded by

danteyda17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Chapter Four

Direct Questions
1. What is a repetition structure?
➢ A structure that causes a section of code to repeat
2. What is a condition-controlled loop?
➢ A loop that uses a true/false condition to control the number of times that it Repeats.
3. What is a count-controlled loop?
➢ A loop that repeats a specific number of times.
4. What is a loop iteration?
➢ An execution of the statements in the body of the loop
5. Does the while loop test its condition before or after it performs an iteration?
➢ Before
6. How many times will 'Hello World' be printed in the following program?
➢ cc count = 10
while count < 1
print ('Hello World')
None. The condition count < 0 will be false to begin with.
7. What is an infinite loop?
A loop that has no way of stopping and repeats until the program is interrupted.
8. Rewrite the following code so it calls the range function instead of using the list.
➢ [0, 1, 2, 3, 4, 5]:
for x in [0, 1, 2, 3, 4, 5]:
print ('I love to program!")
for x in range (6):
print ('I love to program!")
9. What will the following code display?
➢ for number in range (6):
print(number)
0
1
2
3
4
5
10. What will the following code display?
➢ for number in range (2, 6):
print(number)
2
3
4
5
11. What will the following code display?
➢ for number in range (0, 501, 100):
print(number)
0
100
200
300
400
500
12. What will the following code display?
➢ for number in range (10, 5, -1):
print(number)
10
9
8
7
6
13. What is an accumulator?
➢ A variable that is used to accumulate the total of a series of numbers.
14. Should an accumulator be initialized to any specific value? Why or why not?
➢ 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.
15. What will the following code display?
➢ total = 0
for count in range (1, 6):
total total + count
print(total)
15
16. Hat will the following code display?
➢ number 1=10
number 2 = 5
number 1 = number 1 + number 2
print(number1)
print(number2)
15
17. Rewrite the following statements using augmented assignment operators:
➢ a) quantity = quantity + 1
days left = days_left-5
price price* 10
price price/2
quantity += 1
days left=5
price *= 10
price /= 2
18. What is a sentinel?
➢ A sentinel is a special value that marks the end of a list of items.
19. Why should you take care to choose a distinctive value as a sentinel?
➢ A sentinel value must be unique enough that it will not be mistaken as a regular
value in the list.
20. What does the phrase "garbage in, garbage out" mean?
➢ It means that if bad data (garbage) is provided as input to a program, the program will
produce bad data (garbage) as output.
21. Give a general description of the input validation process.
➢ 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.
22. Describe the steps that are generally taken when an input validation loop is used to validate data
➢ 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.
23. What is a priming read? What is its purpose?
➢ 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.
24. If the input that is read by the priming read is valid, how many times will the input validation
loop iterate?
➢ None
25. What is a condition-controlled loop?
➢ Condition controlled loop: uses a true/false condition to control the number of times
that is repeats
26. What is a count-controlled loop?
➢ Count controlled loop: repeats a specific number of times
27. What is an infinite loop? Write the code for an infinite loop?
➢ loops that does not have a way of stopping
➢ Example:
a=5
while a >1:
print(a)
28. How are an accumulator variable and a loop used to calculate a running total?
➢ numbers = [10, 20, 30, 40, 50]
total = 0
for number in numbers:
total += number
print ("The running total is:", total)
The running total is: 150
29. How many iterations would occur in total if a loop that repeats 5 times is nested within a loop
that repeats 4 times?
➢ 20 iterations
30. Why must the value chosen for use as a sentinel be carefully selected?
➢ it must be distictive so that the program will unit recognize the value as a regular value
which could cause error
31. What does the phrase "garbage in, garbage out" mean?
➢ GIGO is a computer science acronym that implies bad input will result in a bad output
32. Give a general description of the input validation process?
➢ input validation: inspecting input before it is processed by the program.
Multiple Choice

1) A controlled loop uses a true/false condition to control the number of?


➢ Condition
2) A controlled loop repeats a specific number of times?
➢ Count
3) Each repetition of a loop is known as a(n)?
➢ Iteration
4) The while loop is a type of loop?
➢ Pretest
5) A(n) loop has no way of ending and repeats until the program is interrupted?
➢ Infinite
6) The operator is an example of a(n) operator?
➢ augmented assignment
7) A(n) variable keeps a running total?
➢ Accumulator
8) A(n) is a special value that signals when there are no more items from a list?
➢ Sentinel
9) Chapter 4 Repetition Structures?
➢ great input, great output
10) The integrity of a program's output is only as good as the integrity of the program's?
➢ Input
11) The input operation that appears just before a validation loop is known as?
➢ priming read
12) Validation loops are also known as?
➢ error traps
True or false
1) A condition-controlled loop always repeats a specific number of times (False)
2) The while loop is a pretest loop (True)
3) The following statement subtracts 1 from x: x = x - 1(True)
4) It is not necessary to initialize accumulator variables (False)
5) In a nested loop, the inner loop goes through all of its iterations for every single
iteration of the outer loop (True)
6) To calculate the total number of iterations of a nested loop, add the number of
iterations of all the loops (False)
7) The process of input validation works as follows: when the user of a program enters
invalid 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 (False)

You might also like