Objectives

Students will be able to:

  • understand that a function can return a value to the program that called it
  • understand that the returned value can be processed by the calling program

Feed The Monkey

The following process happens 20 times:

a zoo keeper gets a bunch of bananas and feeds a monkey.

The monkey tells the zoo keeper how it feels:

  • >15 : "happy"
  • >10 : "satisified"
  • >5 : "disappointed"
  • <=5 : "furious"

The number of times the monkey is "happy" is counted

After the feeding process, the happy count is evaluated. If the happy count is less than 10, the zoo keeper is sacked!

Feed the Monkey

#global variables

#none needed yet!

def feedMonkey(bananas):

if bananas > 5:

feeling = "happy"

elif bananas > 2:

feeling = "Disappointed"

else:

feeling = "Sad"

print(f"{feeling}")

def main():

print("Let's play Feed the Monkey!")

for i in range(20):

randNum = random.randint(0,7)

feedMonkey(randNum)

print("Wanna play again?")

#call main

main()

Feed the Monkey

#global variables

happyCount = 0

def feedMonkey(bananas):

global happyCount

if bananas > 5:

feeling = "Happy"

happyCount = happyCount + 1

elif bananas > 2:

feeling = "Disappointed"

else:

feeling = "Sad"

print(f"{feeling}")

def main():

print("Let's play Feed the Monkey!")

for i in range(20):

randNum = random.randint(0,7)

feedMonkey(randNum)

print(f"The monkey was happy {happyCount} times!")

print("Wanna play again?")

#call main

main()

Feed the Monkey

#global variables

#none needed

def feedMonkey(bananas):

if bananas > 5:

feeling = "happy"

elif bananas > 2:

feeling = "disappointed"

else:

feeling = "sad"

return feeling

def main():

print("Let's play Feed the Monkey!")

happyCount = 0

for i in range(20):

randNum = random.randint(0,7)

result = feedMonkey(randNum)

if result == "happy":

happyCount = happyCount + 1

print(f"The monkey was happy {happyCount} times!")

print("Wanna play again?")

#call main()

main()

Challenge

A game maker needs large wooden dice to be painted. The cost is $7/cm2 if normal paint is used.

It needs a computer program to help calculate the total cost of painting the 6 sides of a large wooden dice.

Function Design 1

A function, getSurfaceArea, takes the width and length of the side of a dice in cms and returns the total surface area ie length*height*6:

Complete the function design.

def getSurfaceArea(l,w):

#your code here

Function Design 2

A function, getpaintQuality(), asks a user which paint they want to use, normal or premium. The user enters their choice.

The function returns their choice.

Complete the function design:

def getPaintQuality():

#your code here

Function Design 3

A function, calculateTotalCost(), takes the surface area of a large wooden dice and a paint quality as parameters. Depending on the paint quality, it calculates and returns the total cost of painting the room.

Premium paint always costs twice as much as Normal paint.

Complete the function design:

def calculateTotalCost(sa, pq):

#your code here

Function Design 4

A function, displayResult(), takes the surface area of a large wooden dice, a paint quality, and a total cost as parameters. It displays the information in a sentence.

It does not return anything.

Complete the function design:

def displayResult(surface, paintQ, tCost):

#your code here

def main()

Write a program which will solve the dice paint problem. Call the functions that you designed.

Every Team Member can have a different solution - the key is to support each other - BE A TEAM!

Return statements

send something back!

 

The Global Challenge!

We know how to create global variables to allow different functions in a program to access the same data.

We also know how to use parameters to help functions share data.

The BIG question is:

Can we write computer programs which do not require global variables?

Task

Create a new file in your IDE called FeedTheMonkey.py

Watch the video and do what it does.

Challenge

The algorithm to feed a monkey 20 times and determine the happy count is going to be repackaged into a function called zooKeeperPerformance

This function will determine the happy count after a zoo keeper feeds a monkey 20 times and returns the count.

The main() function now looks like this:

def main():

keeper1 = zooKeeperPerformance()

keeper2 = zooKeeperPerformance()

bestKeeper = evaluateKeepers(keeper1, keeper2)

displayResult(bestKeeper)

The evaluateKeepers() function takes the happy counts of keeper1 and keeper 2 as parameters.

It processes this data as follows:

if the first parameter is greater than the second parameter, the function returns 1

else if the second parameter is greater than the first parameter, the function returns 2

else it returns 0

The displayResult() function takes an integer as a parameter. It processes the integer as follows:

if the integer is 1 the function displays ZooKeeper 1 keeps their job! ZooKeeper 2 is sacked!

else if the integer is 2 the function displays ZooKeeper 2 keeps their job! ZooKeeper 1 is sacked!

else the function displays Both ZooKeepers keep their jobs!

Code this adaptation.

Tags

module function return identifier parameter pseudocode return typedefinecalldata type