Defining Functions
Python has many useful built-in functions that we use when creating experiments, however often it is still necessary to create our own functions that work specifically with our code. In this section you will learn how to create simple functions in PsychoPy3.
When we create a function, we give it a name and a purpose. For example a wheelbarrow has the name 'wheelbarrow' and the function of 'carrying and transporting items'. Whenever we pick up the handles of the wheelbarrow or put something inside the wheelbarrow, we can move the wheelbarrow.
It's the same with our functions! We can create a function named 'splitter' and we can give it the function of splitting sentences into the individual words. Whenever we put a sentence into the function, the function should split the words from each other. We could try putting numbers into the function but we don't know if that would work and it could result in some errors.
Let's try making the sentence splitter function!
To define the function we start off by using the def
code as done below, so Python knows that we now want to create a new function.
def splitter(words):
Next we write in the desired name of our function, which in our case is splitter
. Then inside the brackets of our function, we type in what kind of input we want our function to deal with. I have just named the input words
, it could be named anything.
Under the function, four spaces forward, we type in what we want the function to do. In our case we would like the function to split our input into separate words. So we use the already existing function in Python split()
on the input words
inside our function. Then we ask the function to print out our sentences after they have been split into words.
def splitter(words):
words = words.split()
print(words)
There we go! Now we have a function that splits sentences into words. If I wanted to split the sentence "Hello there, how are you?" into words, I could simply use my splitter function on this sentence:
splitter('Hello there, how are you?')
And viola! Here is the output we get:
##########Output:##########
['Hello', 'there,', 'how', 'are', 'you?']
We could use the function on other sentences and it still works well.
splitter('Does this work for everything?')
##########Output:##########
['Does', 'this', 'work', 'for', 'everything?']
How about on numbers?
splitter('1 3 5 76 78 8 9')
##########Output:##########
['1', '3', '5', '76', '78', '8', '9']
That works too!
Exercises
Exercise 1:
Create a function that:
Prints out the word "Good day" if it is fed with any of these times: Morning or Midday.
Prints out the word "Good evening" if it is fed with any of these times: Afternoon or Evening.
Hint: use if-elif-else statements.
Exercise 2:
Create a function that loops through any list you feed it, and prints out the value and length of all the items in the list.
Summary of Code
#Here we create a function named 'splitter' that receives an input, then splits that input into single words if the input consists of more than one word. The function then prints out the single words in the input. The function also works for splitting number strings where there are spaces between the numbers:
def splitter(words):
words = words.split()
print(words)
#Here we are feeding some words to the function above:
splitter('Hello there, how are you?')