Python Conditional Function Definition Flashcards

 

card 1 of 10

 

def isString(x):
  if type(x)==str:
    return True
  else:
    return False
>>> isString("wonder")
>>> isString("")
>>> isString([1,2])
>>> isString(5)

 

type(x) could be str, int, float, bool, list, dict, tuple and so forth

True
True
False
False

card 2 of 10

 

def isString(x):
  if type(x)==str:
    return True
    return False
>>> isString("wonder")
>>> isString("")
>>> isString([1,2])
>>> isString(5)

 

type(x) could be str, int, float, bool, list, dict, tuple and so forth

True
True


Note: the blank lines are because isString() did not return an answer!

card 3 of 10

 

def isString(x):
  if type(x)==str:
    return True
  return False
>>> isString("wonder")
>>> isString("")
>>> isString([1,2])
>>> isString(5)

 

type(x) could be str, int, float, bool, list, dict, tuple and so forth

True
True
False
False

card 4 of 10

 

def safeDivide(x,y):
  if y==0:
    return -999
  return x/y
  # Note: previous line should be "return x//y" in Python 3
>>> safeDivide(200,10)
>>> safeDivide(100,10-2*5)

 

just substitute arguments for function parameters and evaluate

20
-999

card 5 of 10

 

def AddOrSubtract(x,y,doAdd):
  if doAdd:
     return x+y
  else:
     return x-y
>>> AddOrSubtract(100,1,False)
>>> AddOrSubtract(100,1,True)

 

substitute arguments for parameters x and y, then evaluate

99
101

card 6 of 10

 

def AddOrMultiply(x,y,doAdd):
  if doAdd:
     return x+y
else:
     return x*y
>>> AddOrMultiply(100,1,False)
>>> AddOrMultiply(100,1,True)

 

observe formatting here

SyntaxError: improper indentation in function definition

card 7 of 10

 

def DivideOrMultiply(x,y,doDivide):
if doDivide:
  return x/y
else:
  return x*y
>>> DivideOrMultiply(100,1,False)
>>> DivideOrMultiply(100,1,True)

 

observe formatting here

IndentationError: expected an indented block

card 8 of 10

 

def isEven(x):
  return x%2==0
def halfString(y):
  if len(y)<1:
    return y
  if isEven(len(y)):
    return y[:len(y)/2]
  return y[:(len(y)-1)/2]
>>> halfString("wonder")
>>> halfString("O")
>>> halfString("amazing")
>>> halfString("")

 

it's tedious, but work through substituting an evaluating, and remember how slice/indexing works

'won'
''
'ama'
''
NOTE: in Python 3, the '/' has to be replaced by '//' or there is an error

card 9 of 10

 

def twicePrint(x):
  print x # should be print(x) in Python 3
  print x # should be print(x) in Python 3
  if len(x)>5:
    twicePrint(x)
>>> twicePrint("hello")
>>> twicePrint("hello?")

 

programmers can make the computer behave badly

hello
hello
RuntimeError: maximum recursion depth exceeded

card 10 of 10

 

def twicePrint(x):
  print x # should be print(x) in Python 3
  print x # should be print(x) in Python 3
  if len(x)>5:
    twicePrint(x[1:])
>>> twicePrint("hello")
>>> twicePrint("hello?")

 

work though substitution of argument for parameter, then make a guess about what Python will do

hello
hello
hello?
hello?
ello?
ello?

 

End of Problem Set

(use browser reload to restart this problem set)