python ch 4

Ace your homework & exams now with Quizwiz!

What will the following code display? for number in range(6): print(number)

0 1 2 3 4 5

What will the following code display? for number in range(0, 501, 100): print(number)

0 100 200 300 400 500

What will the following code display? for number in range(10, 5, -1): print(number)

10 9 8 7 6

What will the following code display? total = 0 for count in range(1, 6): total = total + count print(total)

15

What will the following code display? number 1 = 10 number 2 = 5 number 1 = number 1 + number 2 print(number1) print(number2)

15 5

What will the following code display? for number in range(2, 6): print(number

2 3 4 5

What is an infinite loop?

A loop that has no way of stopping and repeats until the program is interrupted.

What is a count-controlled loop?

A loop that repeats a specific number of times

What is a condition-controlled loop?

A loop that uses a true/false condition to control the number of times that it repeats

What is a sentinel?

A sentinel is a special value that marks the end of a list of items.

What is a repetition structure?

A structure that causes a section of code to repeat

What is an accumulator?

A variable that is used to accumulate the total of a series of numbers

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.

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.

How many times will 'Hello World' be printed in the following program? count = 10 while count < 1: print('Hello World')

None. The condition count < 0 will be false to begin with.

Describe the steps that are generally taken when an input validation loop is used to validate data.

The input is read, and 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.

What is a loop iteration?

What is a loop iteration?

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.

Why should you take care to choose a distinctive value as a sentinel?

Why should you take care to choose a distinctive value as a sentinel?

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.

Rewrite the following statements using augmented assignment operators: a) quantity = quantity + 1 b) days_left = days_left − 5 c) price = price * 10 d) price = price / 2

a) quantity += 1 b) days_left −= 5 c) price *= 10 d) price /= 2

Does the while loop test its condition before or after it performs an iteration?

before

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!')

If the input that is read by the priming read is valid, how many times will the input validation loop iterate?

none


Related study sets

Articles of Confederation Ms P Civics

View Set

AMH Ch 16: Capital and Labor quiz

View Set

A Study on Positive and Negative Effects of Social Media on Society & How Video Games Benefit Your Brain

View Set

AP Euro Chapter 17- Toward a New Worldview

View Set

Chapters 1 Practice Quiz Questions From Learn Smart Quiz Bank

View Set