Creating multiple functions in the same code in Python


Well, let's just say of the newbies in programming and codecademy, i am newest. Almost completely clueless. I have come here knowing i'd learn and find answers. By the way, i'm Nigerian and hold a first degree in electronics engineering. There is a test on Python programming which i must take before January 15 next year, in preparation for which i took up the codecademy Python training. Now i can't get past an exercise where i'm meant to create two functions in the same code, call and return. Some help, please.
You see, there is an organisation over here with this tech revolution thingy; and the test is part of a process to select worthy fellows to join the vanguard team. Now you understand why i came running! Here are the instructions and my wrong code:

First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon!
Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again).
Define a second function called by_three that takes an argument called number.
if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.
Don't forget that if and else statements need a : at the end of that line!
def cube(number):
return number**3
def by_three(number):
if number % 3 == 0:
return cube
return False


The return statement should be,

    return cube(number)

and remove that line from below, return False.


Thank you. I have done that but it's not working. Here's the error message:

Oops, try again. by_three(3) returned instead of 27



The true purpose of this section is to demonstrate object orientation, though it is a minor point of this section remembering tht will help a long way in understanding what it is trying to get you to accomplish.

The exercise is to create two functions and then call on the first function if a number is divisible by 3. This is quite simple to accomplish.

When you are creating these functions remember the OOP(Object-Orientated Programming) is the reason why this works so well in python.

Ok, on to the steps we need to do to accomplish the objective. Remember when programming always go through the steps in a some-what logical way to accomplish your goal. In python a lot of times your code will reflect your steps almost exactly.

Steps: Practice Makes Perfect-Functions

  1. Create a function called cube, which returns the cube of a number
  2. Create a second function called by three which checks to see if a number is evenly divisible by three
  3. If the number is divisible by three use your cube function on it
  4. Finally, if the number does not divide evenly by three return False

Ok, now that we have the steps let's go about programming our functions.

# STEP 1
def cube(number):
    # number * number * number
    return number**3
# Step 2
def by_three(number):
    if number % 3 == 0:
        # STEP 3
        return cube(number)
    # STEP 4
    return False

Does that explain what is needed better?

In reality you would rarely ever use these two functions as they are. You would simply create a single function that cubes the results in the function it's self.


def by_three(number):
    return number**3 if number % 3 == 0 else False

In all code you should complete your given task in the in the most simplistic way you possible can, that way weeks even months or years from now you can still read your code. Also, by doing this you will generally create very low bug count code. Which is always a plus.

If you do not understand any part of this please feel free to ask anything.


Thank you, Owen. It worked! Aren't you a life saver?
I am sorry about the late reply; i got caught up.
This forum is invaluable. I sure am sticking around!


Please is any one there? Can you help me out with writing the python code for this problem?

Create a function manipulate_data that does the following

Accepts as the first parameter a string specifying the data structure to be used "list", "set" or "dictionary"
Accepts as the second parameter the data to be manipulated based on the data structure specified e.g [1, 4, 9, 16, 25] for a list data structure
Based off the first parameter
return the reverse of a list or
add items "ANDELA", "TIA" and "AFRICA" to the set and return the resulting set
return the keys of a dictionary.


Topic was already made. go check this one out i think everyone is giving the code for what you are looking for