3 | Control Statements and Program Development (Self Check)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

number of character positions

(Fill-In) A field width specifies the ___________ to use when displaying a value.

requirements statement

(Fill-In) A(n) ___________ describes what a program is supposed to do, but not howthe program should do it.

flowchart

(Fill-In) A(n) ___________ is a graphical representation of an algorithm.

algorithm, actions, order

(Fill-In) A(n) ___________ is a procedure for solving a problem. It specifies the ___________ to execute and the ___________ in which they execute.

range

(Fill-In) Function ___________ generates a sequence of integers.

35

(Fill-In) If x is 7, the value of x after evaluating x *= 5 is ___________ .

initialization, processing, termination

(Fill-In) Many of the scripts you'll write can be decomposed into three phases: ___________ , ___________ and ___________ .

indefinite repetition

(Fill-In) Sentinel-controlled repetition is called ___________ because the number of repetitions is not known before the loop begins executing.

mean

(Fill-In) The ___________ statistic indicates the average value in a set of values.

median

(Fill-In) The ___________ statistic indicates the middle value in a set of values.

mode

(Fill-In) The ___________ statistic indicates the most frequently occurring value in a set of values.

sequential execution, selection statements, repetition statements

(Fill-In) You can write all programs using three forms of control—___________ , ___________ and ___________ .

In [1]: i = 1 In [2]: j = 2 In [3]: k = 3 In [4]: m = 2 In [5]: (i >= 1) and (j < 4) Out[5]: True In [6]: (m <= 99) and (k < m) Out[6]: False In [7]: (j >= i) or (k == m) Out[7]: True In [8]: (k + m < j) or (3 - j >= k) Out[8]: False In [9]: not (k > m) Out[9]: False

(IPython Session) Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the following conditions display? a. (i >= 1) and (j < 4) b. (m <= 99) and (k < m) c. (j >= i) or (k == m) d. (k + m < j) or (3 - j >= k) e. not (k > m)

In [1]: from decimal import Decimal In [2]: print(f"{Decimal('37.45') * Decimal('1.0625'):.2f}") 39.79

(IPython Session) Assume that the tax on a restaurant bill is 6.25% and that the bill amount is $37.45. Use type Decimal to calculate the bill total, then print the result with two digits to the right of the decimal point.

In [1]: x = 12 In [2]: x **= 2 In [3]: x Out[3]: 144

(IPython Session) Create a variable x with the value 12. Use an exponentiation augmented assignment statement to square x's value. Show x's new value.

In [1]: number1 = 7 In [2]: number2 = 5 In [3]: print(f'{number1} times {number2} is {number1 * number2}') 7 times 5 is 35

(IPython Session) Display an f-string in which you insert the values of the variables number1 (7) and number2 (5) and their product. The displayed string should be: times 5 is 35

In [1]: import statistics In [2]: values = [47, 95, 88, 73, 88, 84] In [3]: statistics.mean(values) Out[3]: 79.16666666666667 In [4]: statistics.median(values) Out[4]: 86.0 In [5]: statistics.mode(values) Out[5]: 88

(IPython Session) For the values 47, 95, 88, 73, 88 and 84, use the statistics module to calculate the mean, median and mode.

In [1]: grade = 85 In [2]: if grade >= 60: ...: print('Passed') ...: Passed In [3]: grade = 55 In [4]: if grade >= 60: ...: print('Passed') ...: In [5]:

(IPython Session) Redo this section's snippets [1] and [2], then change grade to 55 and repeat the if statement to show that its suite does not execute. The next section shows how to recall and re-execute earlier snippets to avoid having to re-enter the code.

In [1]: grade = 80In [2]: if grade >= 90: ...: print('A') ...: else: ...: print('Not A or B') ...: elif grade >= 80: File "<ipython-input-2-033bcba40157>", line 5 elif grade >= 80: ^ SyntaxError: invalid syntax

(IPython Session) Show that a SyntaxError occurs if an if...elif statement specifies an else before the last elif.

In [1]: for count in range(2): ...: value = int(input('Enter an integer: ')) ...: if value % 2 == 0: ...: print(f'{value} is even') ...: else: ...: print(f'{value} is odd') ...: Enter an integer: 10 10 is even Enter an integer: 7 7 is odd

(IPython Session) Use a for statement to input two integers. Use a nested if...else statement to display whether each value is even or odd. Enter 10 and 7 to test your code.

In [3]: for number in range(99, -1, -11): ...: print(number, end=' ') ...: 99 88 77 66 55 44 33 22 11 0

(IPython Session) Use a for statement, range and print to display on one line the sequence of values 99 88 77 66 55 44 33 22 11 0, each separated by one space.

In [4]: total = 0 In [5]: for number in range(2, 101, 2): ...: total += number ...: In [6]: total Out[6]: 2550

(IPython Session) Use for and range to sum the even integers from 2 through 100, then display the sum.

In [1]: total = 0 In [2]: for number in range(1000001): ...: total = total + number ...: In [3]: total Out[3]: 500000500000

(IPython Session) Use the range function and a for statement to calculate the total of the integers from 0 through 1,000,000.

Nothing displays because the step is not negative (this is not a fatal error): In [1]: for number in range(10, 0, 2): ...: print(number, end=' ') ...: In [2]:

(IPython Session) What happens if you try to print the items in range(10, 0, 2)?

In [1]: number1 = int(input('Enter first integer: ')) Enter first integer: 10 In [2]: number2 = int(input('Enter second integer: ')) Enter second integer: 5 In [3]: total = number1 + number2 In [4]: print('The sum of', number1, 'and', number2, 'is', total) The sum of 10 and 5 is 15

(IPython Session) Write Python statements that perform the tasks described by this section's pseudocode. Enter the integers 10 and 5.

In [1]: product = 7 In [2]: while product <= 1000: ...: product = product * 7 ...: In [3]: product Out[3]: 2401

(IPython Session) Write statements to determine the first power of 7 greater than 1000.

False. A fatal logic error causes a script to terminate.

(True/False) A fatal logic error causes a script to produce incorrect results, then continue executing.

True

(True/False) A while statement performs its suite while some condition remains True.

False. Function call range(1, 10) generates the sequence 1 through 9.

(True/False) Function call range(1, 10) generates the sequence 1 through 10.

False. All the statements in a suite must have the same indentation. Otherwise, an IndentationError occurs.

(True/False) If you indent a suite's statements, you will not get an IndentationError.

False. Pseudocode is not a programming language. It's an artificial and informal language that helps you develop algorithms.

(True/False) Pseudocode is a simple programming language.

False. Sentinel-control repetition terminates repetition when the sentinel value is encountered.

(True/False) Sentinel-controlled repetition uses a counter variable to control the number of times a set of instructions executes.


Ensembles d'études connexes

INF 141 / CS 121 Information Retrieval Quiz 4 W18, CS 121 - Quiz 4, Inf 141: Quiz 4, Quiz 3 CS 121, Inf 141: Quiz 3, (CS 121) Quiz #2 Review, CS 121 Final

View Set

Pediatric Nursing - Growth and Development NGN EAQ

View Set

396 - Practice EAQ's (Maternity)

View Set

English : The Quest of the Golden Fleece : Study Questions

View Set