CSE practice problems - week 3
Why might you want to use a compound conditional? Hint, there is more than one right answer. - When you want your code to be faster. - You will get an error message if you don't use a compound conditional. - If two or more things have to be true. - When you want your code to be more concise and easy to read.
- If two or more things have to be true. - When you want your code to be more concise and easy to read.
What is output from the following code? day = "Tuesday" homework_done = True if day == "Saturday" and homework_done == True: print("Go to movie") else: print("Do your homework!") print("Can't wait for the weekend")
Do your homework! Can't wait for the weekend
What does an if statement not do? Evaluates a boolean expression. Evaluates code when the boolean expression is false. Allows your program to make decisions about what to do. Determines if something is True or False.
Evaluates code when the boolean expression is false.
What is wrong with the code below? Hint, there is more than one right answer. if 3 > 4 print("This is true") else print("This is false")
There needs to be a : after the if statement and the else ; The print statements need to be indented
What are the requirements of using an else statement? Hint, there is more than one right answer. There is no indentation Use a : at the end Do not use another boolean expression Code after the else must be indented
all of the statements are true
What data type is used by a conditional to know which part of the code should be executed? Integer Boolean Float String
boolean
What keyword allows you to have more than 2 branches with a single conditional statement?
elif
Which of the following elif statements is correct? elif a < 10: elif: elif a < 10 else if a < 10:
elif a < 10:
Fill in the blanks of the passage below.: A series of elif statements can be more efficient than a series of if statements because the ____ statements will ____ as soon as there is a ____ boolean expression. The ____ statement, however, will keep going even if there is a ____ boolean expression.
elif, stop, true, if, true
true/false: If statements provide the most numeric precision of all the conditional statements.
false
true/false: If statements will perform a set of actions if the boolean expression is false.
false
Place the following code blocks in their proper place. print("10 is not less than 7") if 10 < 7: else: print("10 is less than 7")
if 10<7: print("10 is less than 7") else: print("10 is not less than 7")
create a compound conditional that prints Hello World. Assume the variable color has already been declared and has the value red with the value pink also being an option
if color == 'red; or color == 'pink': print ("hello world")
create an equivalent program that uses elif statements instead of if statements. if my_var < 1: print("my_var is less than 1") if my_var == 1: print("my_var is 1") if my_var > 1: print("my_var is greater than 1")
if my_var <1: print("my var is less than 1") elif my_var == 1 print("my_var is 1") else: print("my_var is greater than 1")
Why might you NOT want to use a compound conditional? Hint, there is more than one right answer. - You have multiple conditions that depend on other conditions involving several variables. - When you want your code to be faster. - You will get an error message if you don't use a compound conditional. - When you want your code to be more understandable and easy to read.
- you have multiple conditions that depend on other conditions involving several variables. - When you want your code to be more understandable and easy to read.
What is a compound conditional?
A conditional that has two or more boolean expressions.
What is output from the following code? day = "Thursday" homework_done = False if day == "Saturday": if homework_done: print("Go to movie") else: print("Do your homework!") print("Can't wait for the weekend")
Can't wait for the weekend
Construct code that will print out messages based on the weather: - "Bring an umbrella!" if it is rainy but not windy - "Wear a rain jacket." if it is rainy and windy - "You might need a coat." if it is cold but not rainy - "Enjoy your day!" if it is not cold and not rainy Do not forget to indent! Assume the following boolean variables have already been declared and initialized with boolean values: rainy, windy, cold
if rainy: if windy: print("wear a rain jacket.") else: print("bring an umbrella!") else: if cold: print("you might need a coat.") else: print("enjoy your day!")
if x > 5: i f x < 20: print(x) convert the code above into a compound conditional.
if x > 5 and x < 20: print(x)
Complete the if statement below so that it will determine if the variable num is greater than 100. Note, do not use parentheses when constructing the conditional. ___ num > 100__ print("num is greater than 100")
if, :
list the terms below into the correct order when evaluating a conditional. elif, if, else
if, elif, else
Rearrange the code below to build a proper elif statement: else: print("less than 5.") my_var = 7 print("greater than 10.") print("between 5 and 10.") if my_var < 5: elif my _Var <= 10:
my_var = 7 if my_var < 5: print("less than 5.") elif my _Var <= 10: print("between 5 and 10.") else: print("greater than 10.")
true/false: If statements are the simplest of all the conditionals.
true
true/false: If statements only ask if a boolean expression is true.
true
Which of the following type of questions is the most accurate to describe what happens with branching in a computer program? Fill in the blank question Yes/No question Multiple Choice question
yes/no question
