You are on page 1of 5

Ajero, Cassandra Jazer Ross G.

BSIT-2104

1. Consider the following program:


temperature= float(input("Enter the temperature of H2O at 1 ATM pressure
in degrees Celsius: "))
if temperature == 100:
print("The water is boiling")
elif temperature >0 and temperature < 100:
print("The water is not boiling.")
elif temperature == 0:
print("The water could be ice or a mixture of ice and water.")
elif temperature < 0:
print("The water has turned to ice")
else:
print("you have superheated steam")

a. What new keyword is used in this program?


Answer: and

b. What would the output be if:

 The water temperature was 100°C


Answer: The water is boiling
 The water temperature was 0°C
Answer: The water could be ice or a mixture of ice and water
 The water temperature was 50°C
Answer: The water is not boiling.
 The water temperature was 100.1
Answer: you have superheated steam

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?

temperature = float(input("Enter the temperature of H2O at 1 ATM pressure in degrees


Celsius: "))

if temperature == 100:

print("The water is boiling")


#The elif temperature == 37: should be place here because if you put it behind the “elif
temperature > 0 and temperature < 100:” it will not read the elif temperature == 37:

elif temperature == 37:

print("The water is at human body temperature.")

elif temperature > 0 and temperature < 100:

print("The water is not boiling.")

elif temperature == 0:

print("The water could be ice or a mixture of ice and water.")

elif temperature < 0:

print("The water has turned to ice")

else:

print("You have superheated steam")

d. Does the placement of an addition elif clause matter?

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.

e. When is the code associated with the else statement executed?

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.

Operator Example Explanation

(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.

The ‘not’ operator is used to negate a single condition.


not not (credits> 120)
It reverses the truth value of the condition.

3. Assume the value of the variable numBooks is 40. State the values of each of
the Boolean expressions that include a compound condition.

Expression Boolean Value

(numBooks > 5) and (numBooks < 100) True

(numBooks < 5) or (numBooks > 100) False

not(numBooks * 10 == 100) True

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)

1 110 1.9 1.48 False

2 120 2.0 1.9 False

3 120 2.0 2.0 True

4 120 1.5 2.0 False

5 125 2.5 3.0 True

6 150 1.4 2.4 False

7 125 2.2 3.2 True


Expression Result
Data Set numCredits majorGPA overallGPA
(True of False)

8 225 1.5 2.0 False

9 85 2.5 3.0 False

10 125 3.5 2.0 True

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)

You might also like