Skip to content

4. Functions, Parameters, Returns

For each of the tasks below, copy the code into EdPy and follow the instructions.

4.1 Functions

Instructions

Call the beeping function so that the Edison beeps 5 times.

Code - Click to expand
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V3

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM
#--------Your code below-----------

# beeping(num) will beep 'num' times
def beeping(num):
    for i in range(num):
        Ed.PlayBeep()
        Ed.TimeWait(500, Ed.TIME_MILLISECONDS)

# call the beeping function to make Edison beep 5 times    
beeping(5)

4.2 Parameters

Instructions

Modify the code below so that the function is complete and working.

Code - Click to expand
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V3

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM
#--------Your code below-----------

# moves Edison forward by the parameter distance
def move_forward():
    Ed.Drive(Ed.FORWARD, Ed.SPEED_3, distance)

move_forward(5)

4.3 Returns

Instructions

Complete the code below as stated in the comments.

Code - Click to expand
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#-------------Setup----------------

import Ed

Ed.EdisonVersion = Ed.V3

Ed.DistanceUnits = Ed.CM
Ed.Tempo = Ed.TEMPO_MEDIUM
#--------Your code below-----------

# this function returns a number from one to six
def get_number():
    return Ed.Random(1, 6)


# beeping(num) will beep 'num' times
def beeping(num):
    for i in range(num):
        Ed.PlayBeep()
        Ed.TimeWait(500, Ed.TIME_MILLISECONDS)

#---------------
# run get_number() and use the returned value in the beeping(num) function