3. Conditional Execution Flashcards

1
Q

body

A

The sequence of statements within a compound statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

boolean expression

A

An expression whose value is either True or False.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

branch

A

One of the alternative sequences of statements in a conditional statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

chained conditional

A

A conditional statement with a series of alternative branches.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

comparison operator

A

One of the operators that compares its operands: ==, !=, >, =, and <=.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

conditional statement

A

A statement that controls the flow of execution depending on some condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

condition

A

The boolean expression in a conditional statement that determines which branch is executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

compound statement

A

A statement that consists of a header and a body. The

header ends with a colon (:). The body is indented relative to the header.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

guardian pattern

A

Where we construct a logical expression with additional comparisons to take advantage of the short-circuit behavior.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

logical operator

A

One of the operators that combines boolean expressions: and, or, and not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

nested conditional

A

A conditional statement that appears in one of the branches of another conditional statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

traceback

A

A list of the functions that are executing, printed when an exception occurs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

short-circuit

A

When Python is part-way through evaluating a logical expression and stops the evaluation because Python knows the final value for the expression without needing to evaluate the rest of the expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Rewrite your pay computation to give the employee 1.5 times the hourly rate for hours worked above 40 hours.

Enter hours: 45
Enter Rate: 10
Pay: 475.0

A

> > > hours = int(input(‘Enter Hours: ‘))

> > > rate = float(input(‘Enter Rate: ‘))

> > > extra_time = hours - 40

> > > if hours > 40:

… pay = (hours - extra_time) * rate + extra_time * rate * 1.5

… else:

… pay = hours * rate

> > > print(‘Pay:’,pay)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Rewrite your pay program using try and except so that your program handles non-numeric input gracefully by printing a message and exiting the program. The following shows two executions of the program:

Enter Hours: 20
Enter Rate: nine
Error, please enter numeric input

Enter Hours: forty
Error, please enter numeric input

A

> > > try:
… hours = int(input(‘Enter Hours: ‘))
… rate = float(input(‘Enter Rate: ‘))
… except:
… print(‘Error, please enter numeric input’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write a program to prompt for a score between 0.0 and 1.0. If the score is out of range, print an error message. If the score is between 0.0 and 1.0, print a grade using the following table:

Score        Grade
>= 0.9            A
>= 0.8            B
>= 0.7            C
>= 0.6            D
< 0.6              F

~~~

Enter score: 0.95 A ~

Enter score: perfect
Bad score

Enter score: 10.0
Bad score

Enter Score: 0.75
C

Enter score: 0.5
F

Run the program repeatedly as shown above to test the various different values for input.

A

1) try:
2) score = float(input(‘Enter 3) Score: ‘))
4) except:
5) print(‘Bad Score’)
6) quit()

7) if score > 0.0 and score < 1.0 :
8) if score >= 0.9:
9) print(‘A’)
10) elif score >= 0.8 :
11) print(‘B’)
12) elif score >= 0.7 :
13) print(‘C’)
14) elif score >= 0.6 :
15) print(‘D’)
16) else :
17) print(‘F’)
18) else :
19) print(‘Bad Score’)