Conditional statement traces¶
You can solve this exercises manually. Then you can check your answers using a Python shell or the Online python tutor tool.
Write the trace of this function. Could it be simplified ?. How ?
def f1(a): if a > 0 : b = a - a elif a < 0 : b = -a + a else : b = 0 c = a - b a = a - c return a, b, c
Write the trace of this function.
def f2(a): if a > 0 : b = -a else : b = a c = a - b h = c == 0 or c > 0 return b, c, h
Which is the value returned by the following function for these calls:
f3(7),f3(20),f3(-36),f3(36)andf3(9)?def f3(a): if a > 0 : if a%2 == 0: if a%3 == 0 : b = a + 6 else: b = a + 2 else : b = 6 else : b = 0 return b
Discuss whether or not the following functions
f4andf5return the same value for all possible values ofxandy.def f4 (x, y): if x == y: z = 0 elif x < y: z = -1 else: z = 1 return z def f5 (x, y): if x == y: z = 0 if x < y: z = -1 else: z = 1 return z