Python first three multiples function

hello fellow students !
I am struggling with this exercise… I wonder if anyone can give a hint…

def first_three_multiples(num):

If I want to find a multiple of a number I should start multiplying the number by 0, 1, 3…
am I wrong?

the first three multiples of a number are 1, 2, 3.

Don’t the directions state, “Write a function named first_three_multiples() that has one parameter named num. This function should print the first three multiples of num. Then, it should return the third multiple. For example, first_three_multiples(7) should print 7, 14, and 21 on three different lines, and return 21.”

3 Likes

I’m also stuck in this. Did you find the answer?

Hi, welcome to the Forums!
I can understand that functions can be challenging at times. You just have to break it down into language that makes sense to you. Draw it out if you need to. (I do this for more complicated things).

What have you tried? What has you stuck? Any error messages?

Define a function called “first_three_multiples” which takes one parameter, “num”. Think of this–the formal parameter-- as a placeholder. It’s empty. Then, when you call the function, you pass something (data) through it (it’s called a argument). [Longer explanation: Python assigns the formal parameter name with the actual parameter data].

There’s also a hint in the directions itself.
Directions:
“…This function should print the first three multiples of num. Then, it should return the third multiple.”

What does this tell us? How would you write that out? Take a stab at it! :slight_smile:

4 Likes

def first_three_multiples(num):
m = 3
print (list(range(10, (m+1)*num, num)))
return num[-1]

TypeError: ‘int’ object is not subscriptable

Traceback (most recent call last):
File “script.py”, line 10, in
first_three_multiples(0)
File “script.py”, line 4, in first_three_multiples
print (list(range(10, (m+1)*num, num)))
ValueError: range() arg 3 must not be zero


Hello @core0785419896, welcome to the forums! Is num a string or an array in this function? If not, should you be using num[-1]?

Hello @core0785419896!

Welcome to the forums!
Maybe you’re thinking a bit too complicated…
The reason you are getting your TypeError is because since num is an integar, you can’t use indexes. This is supposed to return the first 3 multiples. There are multiple ways you can do this.

If you need any more help, please @ me and I’ll respond in a few hours!

Hi, welcome to the forums.
Check out my response above your post. :slight_smile:

1 Like