External Exam Download Resources Web Applications Games Recycle Bin

Functions

simple functions.py

def hello():
  print('gday mate!')

def add_two_nums(a, b):
  return a + b

def lower_the_capital(x):
  y = x.lower() 
  return y 

def repeat_my_letter(capital, timesRepeated):
  letter = lower_the_capital(capital) 
  return letter * timesRepeated 


hello()
#----------------
print(add_two_nums(1, 2))
print(add_two_nums(7, 10))
#----------------
print(repeat_my_letter("A",5))


harder functions.py

def getAge():
  return int(input("how old r u?"))

def under18(currentAge):
  if(currentAge < 18):
      return True
  else:
      return False

def deposit(amt):
  global balance
  balance += amt 

#-----------------------

myAge = getAge()
if under18(myAge):
  print("you are under 18")
else:
  print("you are 18 or older")

balance = 100
deposit(15) 
print(balance)


  1. define a function called findMax(A, B) takes two numbers and returns the larger of the two

  2. continue the bank example by defining a function called withdraw(amt) and balance (which refers to a global variable balance)

  3. Define a function to find the middle number from 3 parameters:
    def find_middle(a, b, c): 
      # your code here to return middle number 
      # instead of just 0 
    return 0
    So if I use your function in this way:
    m = find_middle(4, 1, 2) 
    print(m)
    Your program should print 2