Section 5.3 Assessment: Conditionals
Subgoals for writing conditionals:.
-
Define how many mutually exclusive paths are needed
-
Order paths from most restrictive to least restrictive
-
Write conditional statement with Boolean expression
-
Follow with true path including action in indentation
-
Repeat 3 and 4 until all paths are included
-
(Optional) Follow with else path including action in indentation
Exercises Exercises
2.
3.
4.
Q4: Enter the value of each variable after the following code is executed:
a = 0
b = 1
c = 2
if a < c:
a = c
if 2 * b == c:
c = 0
elif 2 * b < c:
b = 0
else:
a = 0
if c:
c = 2*(b + a)
elif b:
b = 2*(a + c)
elif a:
a = 2*(b + c)
5.
Put the code in the right order to create a program that will ask the user for a number between -20 and 20 (inclusive).Print the number. Then check if the number is positive, negative, or neither. Print the result.
6.
Put the code in the right order to create a program that will generate a random number (1 to 100 inclusive), and print the number. Check if the number is a multiple of 2 and print the result. Then check if the number is a multiple of 3 and print the result. Then check if the number is a multiple of 5 and print the result.
You have attempted 1 of 6 activities on this page.