Very bottom is the correct code. Num is not defined with a default value so how does it produce 10, 20, 30?
I wrote the code to say:
def first_three_multiples(num):
num = 123
return num *3
print(num *1)
print(num *2)
print(num *3)
The above said num wasn’t defined so I changed it to:
def first_three_multiples(num):
return num 3
num = 12*3
print(num *1)
print(num *2)
print(num *3)
Please see How do I format code in my posts? as the forums will remove indentation and such which makes code very difficult to read, especially for an indentation reliant language like Python.
In that example it doesn’t need a default, you pass a value of 10 as the argument for num. The default would only matter if you didn’t pass an argument.
Be aware that you’ll have problems writing statements after return as they’ll never be executed, if return is executed then the currently executing function exits at that point.
So just so I am tracking when I uncommented those calls that became the argument for the function?
Trying to wrap my head around calling the function when the calls aren’t indented because I keep thinking that the indented portion only applies to the function. So I can use any argument outside of the function (not indented) as long as I reference the function which would change the parameter to any argument I desire?
It’s the body of the function that must be indented (code that should run only when the function is called). The actual calls to the function do not have to be indented (unless part of some code that already requires indentation). As for the argument, I think you’ve got the right idea. If you pass an argument of 10 then you basically assign 10 to num inside the function. If it helps the following is perfectly valid syntax-
first_three_multiples(num=10)
This would be passing a keyword argument, in this example it’s exactly the same as first_three_multiples(10). You’ll likely learn more about it in due course. The main point being that you can make multiple calls and num (inside the function) is assigned to whatever you pass…
Indentation is used for a few things in Python, like if statements and for loops. For example the indentation before the call to first_three_multiples is necessary for the syntax of the if statement below-
x = 3
if x > 5:
first_three_multiples(2)
Another example would be indentation used for the body of classes and functions. Here the first function called double is indented as it is part of the body for another function: