Display the Doubles (Solution)
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.
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.
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.
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.
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: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.