Dec 03, 2024 | 286 words | 3 min read
6.2.2. Task 2#
Learning Objectives#
Determine the output of a program through hand tracking
if
-elif
-else
structures and UDFs. Investigate
logical operators in Python.
Task Instructions#
For the exercises below, follow the instructions and determine the output of the code. Save your answers in the PDF file named py2_team_teamnumber.pdf, that you started in Section 6.2.1, where you will also be saving your flowcharts for the other team tasks.
Exercise A (if-elif-else)#
The Python code in Listing 6.1 is typed in after
defining x
and y
. Assume that all other variables are
cleared before the commands are executed.
if x <= 50:
z = 4
if y < 30:
z = x*y
elif y >= 100:
z = x+y
elif y >= 60:
if x > 80:
z = x
elif y > 50:
z = y
else:
z = z*2
print('z =', z)
What will appear on the screen when the following Python commands are executed before the code in Listing 6.1 is entered?
>>> x = 40 >>> y = 40
>>> x = 20 >>> y = 20
>>> x = 55 >>> y = 20
Exercise B (functions)#
Use hand tracking to answer the following questions about Python program in Listing 6.2. Check your work by running the code in Python's interactive mode.
What is printed to the console window?
What variables are in the global namespace, and what are their values?
def myFun1(in1, in2):
x = in1
y = in2
out1 = 6
out1, out2 = myFun2(x, y)
print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}')
return out1
# --------------------------------------------------------
def myFun2(in2, in1):
a = in1*3
b = in2*2
out1 = a+b
out2 = a-b
print(f'in1={in1:.0f}, in2={in2:.0f}, out1={out1:.0f}, out2={out2:.0f}')
return out2, out1
# --------------------------------------------------------
def myFun3():
var1 = 1
var2 = 2
output = 3
print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')
output = myFun1(var1, var2)
print(f'var1={var1:.0f}, var2={var2:.0f}, output={output:.0f}')
# --------------------------------------------------------
var1 = 4
var2 = 2
myFun3()
print(f'var1={var1:.0f}, var2={var2:.0f}')
Exercise C (logic operators)#
Create a table with four columns labeled: logic operations, hand calculation, Python calculation, and differences. You will add this table to the PDF created in the previous exercises (py2_team_teamnumber.pdf).
Consider the logic operations in Listing 6.3 assuming that
a = True
and b = False
.
Copy the logic operations to the first column of the table.
Without using Python, determine the value of each logic operation by hand and record the results in the second column.
Perform the same logic operations in Python’s interactive mode, and record the results in the third column.
In the fourth column, explain any differences between the hand calculation and the Python calculation.
Note
In many programming languages, including Python, the integers
\(1\) and \(0\) become True
and False
respectively
when converted to boolean values. In these comparisons you may assume \(1\)
to be equivalent to True
, and \(0\) to be equivalent to
False
.
1a and b
2a or b
3a == b
4(a and b) == (a or b)
5a != b
6a > b
7a >= b
8a = b