Professional Documents
Culture Documents
Ajero Cassandra Jazer Ross G.
Ajero Cassandra Jazer Ross G.
BSIT-2104
c. You can use the elif as many times as you need. Suppose you wanted to add an option
that indicated that the water is 37°C and is human body temperature. Where would you
add it? What would that code look like?
if temperature == 100:
elif temperature == 0:
else:
Yes, putting an additional elif matters. Because python checks conditions from top
to bottom, it stops and executes the first true condition it finds in the if/elif chain,
and subsequent elif and else clauses will be skipped.
The code under else runs when no prior conditions in elif are met.
f. Is the use of the else statement mandatory when creating an if/elif statement? Explain
your answer.
The use of the else statement is not mandatory when creating an if/elif statement. If
you want to handle all possible cases and provide a default action when none of the
conditions are met, you can use an else statement. Leave it out if not needed for
clarity.
2. Sometimes you want to include more than one condition to determine which code segment is
executed. You can use the following logical operators to create compound conditions.
Examine each operator and a sample of its use and provide an explanation of how the operator
works.
(age >= 17) and (hasLicense = The ‘and’ operator combines two or more conditions and
and
= true) returns True only if all of the conditions are True.
(cost < 20.00) or (shipping = = The ‘or’ operator combines two or more conditions and
or
0.00) returns True if at least one of the conditions is True.
3. Assume the value of the variable numBooks is 40. State the values of each of
the Boolean expressions that include a compound condition.
4. Suppose you want to determine if a student is ready to graduate. The 3 criteria for graduation
are that the student has earned at least 120 credits, their major GPA is at least 2.0 and their
general GPA is also at least 2.0. Which Boolean expression (next page) would be the correct test
for the Python code?
The Missing Boolean expression is(?)
a. numCredits >= 120 or majorGPA >= 2.0 or overallGPA >= 2.0
b. numCredits > 120 and majorGPA > 2.0 or overallGPA > 2.0
c. numCredits > 119 and majorGPA >= 2.0 and overallGPA >= 2.0
Answer: d. numCredits >= 120 and majorGPA >= 2.0 and overallGPA >= 2.0
5. Enter and execute the program in #14. Include your choice for the correct Boolean expression
and create several sets of data to test all possibilities for the Boolean expression. List the data
you used to test all possibilities for the expression and explain your choices.
Expression Result
Data Set numCredits majorGPA overallGPA
(True of False)
6. Write a program that converts the temperature in Celsius to Fahrenheit. Have the program
output two columns. In the first column, it prints out the temperature in Celsius, and the second
in Fahrenheit. Have the data print out all integer values of Celsius from 0 to 100 in 3
degree increments. Have the data print out the Fahrenheit to 1 decimal
Sample Output (in 4 degree increments for Celsius)