FAQ: Code Challenge: Python Functions - First Three Multiples

This community-built FAQ covers the “First Three Multiples” exercise from the lesson “Code Challenge: Python Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

FAQs on the exercise First Three Multiples

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Write your first_three_multiples function here:

def first_three_multiples(num):
print(num)
print(num2)
print(num
3)
return num*3

Uncomment these function calls to test your first_three_multiples function:

first_three_multiples(10)

should print 10, 20, 30, and return 30

first_three_multiples(0)

should print 0, 0, 0, and return 0

On the above code, I am not sure why the 5th line line, " return num*3 " is necessary, when you run the function, it returns, 10,20,30.

I found this one to be an odd exercise. I knew while completing it that there were better ways to write the function, but couldn’t think of anything except for the following:

Write your first_three_multiples function here:

def first_three_multiples(num):
print(num, num + num, num + num + num)
return num + num + num

Running this worked and met the requirements to complete this portion of the lesson, but for practical application, how could I write this better? For example, how would I write this function if I wanted to see the first 50 multiples and have it return or print the 27th, 32nd and 50th? My solution above would still work but would require a LOT of "+ num"s. Obviously the better choice is to keep your code as simple and condensed as possible.

I feel silly asking but for some reason I’m not connecting the dots on this one…

Returning three values implies a list, or a tuple sent back to the caller.

return num, num * 2, num * 3

Given that as a return value, we can use the unpack operator on either list or tuple.

>>> def first_three_multiples(x):
	return x, x * 2, x * 3

>>> first_three_multiples(10)
(10, 20, 30)
>>> print (*first_three_multiples(10), end=' ')
10 20 30 
>>> 
>>> def first_three_multiples(x):
	return [x, x * 2, x * 3]

>>> print (*first_three_multiples(10), end=' ')
10 20 30 
>>> 

The lesson expects only a single value on return, the third multiple, and for us to print the values in the loop.

for x in range(num, num * 4, num):
    print (x, end=' ')
return x

Absolutely no reason to feel silly! We’re all learning here!

Instead of writing

def first_three_multiples(num):
print(num, num + num, num + num + num)
return num + num + num

you can just use a num, a num*2, and a num*3 (Also return num*3)
Like this

def first_three_multiples(num):
print (num)
print (num*2)
print (num*3)
return num*3

Multiplying is just simplified addition, so here we can just use multiplying and be neat and simple - instead of adding all of them.

How do we ‘simplify addition’? Multiplication is repeated addition and only exists because of the additive property of numbers.

You’re completely right, I did mean repeated! My apologies, it was pretty late when I replied to that, didn’t completely get to convey my thoughts haha.
But yeah, the code should still be correct, regardless of my language barrier blunder.
(When I said simplified, I meant less items in code, replying to their question on how to keep code “as simple and condensed as possible.”)
Thanks for the keen eye!

1 Like

Issue with the UI. I uncommented the (10) and got the correct answer BUT the UI complains that I should have returned 15.
See screenshot
Google Photos

wait, there’s more! It gets better/worse… I supply the ‘correct answer’ and the UI still complains that it returned 15 and expected 15!
Google Photos

1 Like

Is there a reason to be casting everything to str? print() can handle numbers just fine, and we should be returning a number to the caller, not a string.

Yep. Good point re: str(print). You are right. It is unnecessary.
I have been doing a lot of formatting str and num in sentences, I got carried away.

1 Like

This can be helpful when dealing with more of these properties in the future:

def first_three_multiples(num):
  for m in (1,2,3):                            #loops the next line for each variable in m
    print(num*m)
  return num*3

had this problem as well. nothing i did would let me clear it. quite frustrating