Display the Doubles (Solution)
00:00
Start again by writing the function definition def doubles
, opening parenthesis, and then again, you take a number as an argument. So you add the parameter num
, and in the next line, return num * 2
.
00:17
Fair enough. Save the file and run the module. And when you call the double()
function with the argument 3
, you get an error because it’s doubles()
not double()
.
00:29
So when you call doubles()
with the argument 3
, you get 6
back. Perfect. That seems to work. So that means that you have defined the doubles()
function.
00:39
It takes a number as its input and doubles it. So far, so good. You can remove that comment then and tackle the next part. To use this doubles()
function in a loop, before getting into coding right away, let’s take a moment to think about it.
00:56
You need to double the number 2
three times. That means you need to save the current value somewhere. So I think it’s a good idea to create a variable that starts as the integer 2
and then update this number with each for
loop step.
01:15
So again, the variable name is, in my case, my_num
, and the value is 2
because the task is to double the number 2
three times.
01:27
And now you can start the for
loop with for _
(I will say something about the underscore in a moment) in range()
, and then you want to start with 0
as the start argument, up to 3
,
01:41
but maybe you already know the range()
function, so it always starts with 0
. So you can actually ditch this first argument and only call range()
with the argument 3
.
01:53
And then in the next line, my_num = doubles()
. So now you need to call the doubles()
function with the current value of my_num
02:06
and then print the current value of my_num
. So at this point, my_num
is doubled.
02:14
Save the file and run the module. And now you can see that the result is 4
, 8
, and 16
. That looks right to me.
02:23
Okay, let’s recap a moment. And I will start with these underscores. So the for
loop at the moment is for _
in range(3)
.
02:32
And you might wonder why I’m using this underscore here. The reason for this is in Python, if you have a so-called throwaway variable that you need to use to define the for
loop, for example, but you don’t use in your actual code, it’s the convention to use an underscore.
02:49
And this indicates the actual value of this variable doesn’t matter. Right now I just needed to make the for
loop work. And as you can see in the body of the for
loop, I don’t use the underscore as a variable there.
03:02
It was different than a lesson before when I used the i
because I wanted to use the i
as the value of the for
loop step in my calculations, but in this case it doesn’t matter.
03:14
That’s why the underscore is there. And then in the for
loop body, you are assigning the result of the doubles()
function to my_num
and then print it.
03:24
As you can see in the output, my_num
is doubled with each for
loop step.
03:31 So yeah, this one might have been a bit more challenging as an exercise, but that’s a good way to get a little bit into the details of loops because you definitely need it in the challenge that you will tackle in the upcoming lesson.
Become a Member to join the conversation.