Looks like no one added any tags here yet for you.
What is a control structure?
A logical design that controls the order in which a set of statements execute.
What is a decision structure?
It is a program structure that can execute a set of statements only under certain circumstances.
What is a single alternative decision structure?
Provides a single alternative path of execution. If the condition that is being tested is true, the program takes the alternative path.
What is a Boolean expression?
An expression that can be evaluated as either true or false.
What types of relationships between values can you test with relationship operators?
You can determine whether one value is greater than, less than, greater than or equal to, less than or equal to, equal to, or not equal to another value.
Write an if statement that assigns 0 to x if y is equal to 20.
if y == 20:
x=0
Write an if statement that assigns 0.2 to commissionRate if sales is greater than or equal to 10000.
if sales >= 10000:
commissionRate = 0.2
How does a dual alternative decision structure work?
It has two possible paths of execution; one path is taken if a condition is true, and the other path is taken if the condition is false.
What statement do you use in Python to write a dual alternative decision structure?
if-else
When you write an if-else statement, under what circumstances do the statements that appear after the else clause execute?
When the condition is false.
What would the following code display?
if ‘z’ < ‘a’:
print (‘z is less than a.’)
else:
print (‘z is not less than a.’)
z is not less than a.
What would the following code display?
s1= ‘New York’
s2=’Boston’
if s1 > s2:
print (s2)
print (s1)
else:
print (s1)
print (s2)
Boston
New York
Convert the following code to an if-elif-else statement:
if number == 1:
print (‘One’)
else
if number == 2:
print (‘Two’)
else:
if number == 3:
print (‘Three’)
else:
print (‘Unknown’)
if number == 1
print (‘One’)
elif number == 2:
print(‘Two’)
elif number == 3:
print (‘Three’)
else:
print (‘Unknown’)
What is a compound Boolean expression?
It is an expression that is created by using a logical operator to combine two Boolean subexpressions.
The following truth table shows various combinations of the values true and false connected by a logical operator. Complete the table by circling T or F to indicate whether the result of such a combination is true or false.
Page 139 & Answer Key
F
T
F
F
T
T
T
F
F
T
Assume the variables a= 2, b=4, and c=6. Circle T or F for each of the following conditions to indicate whether its value is true or false.
a==4 or b >2
6<= c and a >3
1 != b and c != 3
a >= -1 or a <= b
not (a > 2)
T
F
T
T
T
Explain how short-circuit evolution works with the and and or operators.
The and operator: if the expression on the left side of the and operator is false, the expression on the right side will not be checked
The or operator: If the expression on the left side of the or operator is true, the expression on the right side will not be checked.
Write an if statement that displays the message “The number is valid” if the value referenced by speed is within the range 0 through 200.
if speed >= 0 and speed <= 200:
print (‘The number is valid’)
Write an if statement that displays the message “The number is not valid” if the value referenced by speed is outside the range 0 through 200.
if speed < 0 or speed > 200:
print(‘The number is not valid’)
What values can you assign to a bool variable?
True or False
What is a flag variable?
A variable that signals when some condition exists in the program
How do you get the turtle’s X and Y coordinates?
You can use the turtle.xcor() and turtle.ycor() functions.
How would you determine whether the turtle’s pen is up?
You would use the not operator with the turtle.isdown() function, like this:
if turtle.isdown():
statement
How do you get the turtle’s current heading?
You use the turtle.heading() function.
How do you determine whether the turtle is visible?
You can use the turtle.isvisible() function.
How do you determine the turtle’s pen color?
How do you determine the current fill color?
How do you determine the current background color of the turtle’s graphics window?
You use the turtle.pencolor() function to determine the pen color.
You use the turtle.fillcolor() function to determine the current fill color.
You use the turtle.bgcolor() function to determine the current background color of the turtle’s graphics window.
How do you determine the current pen size?
You use the turtle.pensize() function.
How do you determine the turtle’s current animation speed?
You use the turtle.speed() function.