Can the first_three_multiples function be written so that it doesn't have three print statements?

Or this?:

def first_three_multiples(num):
    for x in range(1, 4):
        print(num * x)
    return print(num * 3)

This works too, what do you think?

def first_three_multiples(num):
  return print(f"{num}\n{num * 2}\n{num * 3}\n{num * 3}")
2 Likes

2 posts were split to a new topic: Does my code not print?

One of the solutions is to use chr(i) method as below :

def first_three_multiples(num):
  return print(str(num*1) + chr(10) + str(num*2) + chr(10) + str(num*3))

There is some confusion in these answers between print() and return.

return <expression> is a statement that directs the Python interpreter to:

  1. Evaluate expression, and pass the resulting value to the calling statement. If the calling statement does not do something with the value, that value is lost.
  2. Halt execution of the function

print(<expression>) is a function that first, evaluates the expression within its parentheses, and then turns the resulting value into a stream of text, which is sent to the standard input-output device, usually the computer screen. The print() function has no return statement written into it, and so, as with every Python having no return statement, returns the value None.

Let’s look at return

# First, return. Here is a function to return 2 * value of x.
# It will show us that it is running by means of a print() function
def mult_by_two_return(x):
    print("The function is now running")
    return x * 2

# Now let us call the function three times:    
mult_by_two_return(5)   # 1. This calling statement does not do anything with the value 
p = mult_by_two_return(5)  # 2. This calling statement assigns the returned value to a variable
print(p)   #  Now we can print the value stored in the variable
print(mult_by_two_return(5))   # 3.  Or, we can put the calling statement in a print function

# Output to screen:
The function is now running  # call number 1. The returned value 10 is lost.
The function is now running  # call number 2
10    # Screen display from print(p)
The function is now running  # call number 3, which was print(<expression>)
10    # Screen display from print(<expression>)

Now, let’s look at print()

# Here is a function to print 2 * value of x.
# It will show us that it is running by means of a different print() function
def mult_by_two_print(x):
    print("The function is now running")
    print(x * 2)

# Now let us call the function three times:   
mult_by_two_print(5)    # Call 1 - 
p = mult_by_two_print(5)   # Call 2 - Output value is assigned to a variable
print(p)    # We print the value passed to the variable
print(mult_by_two_print(5))   # Call 3  - First, the function is run, then the returned value is sent to print()

# Output to screen:
The function is now running  # Running # 1
10           # function prints 10 as it runs
The function is now running  # running # 2
10           # function prints 10 as it runs
None     # The value passed to the variable p in None
The function is now running   #  call number 3, which was print(<expression>)
10           #   call number 3 prints 10 as the function runs
None     # this is the returned value sent to print
5 Likes

A post was merged into an existing topic: My number is correct but it isn’t accepting it?

So there is always a returned value? and if it’s not set then it returns ‘none’ ?

Actually, it returns the value None. But yes, that is correct

1 Like

Does the print statement print(num * 1, num * 2, num * 3) work? It does not throw an error, but is this an incorrect way to solve this question?

1 Like

Before seeing that the Hint on this exercise asks to have three separate print() functions inside the main function, I solved the problem using a while loop as shown below.

def first_three_multiples(num):
  mltpl = 1
  thrd = 0
  while mltpl <= 3:
    num2 = num * mltpl
    if mltpl == 3:
      thrd = num2
    mltpl += 1
    print(num2)
  return thrd

This works for me

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

1 Like

It works~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Thanks! Your code is the easiest to understand :grinning:

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
1 Like

What is the “f” in youre return line actually do?
Before reading the hint i tried to do the pring in a single line but it didnt work for me:

def first_three_multiples(num):
  print(num*1, \n num*2, \n num*3)
  return num*3

I got a “SyntaxError” saying - “unexpected character after line continuation character” for my Print line.


def first_three_multiples(num):
  x = num
  y = num * 2
  z = num * 3
  print(str(x), "\n" + str(y),"\n" + str(z))
  return z

I agree the easiest to understand.
I had written:
"
def first_three_multiples(num):
first = num1
second = num
2
third = num*3
print(first,second,third)
return third
"
Which is far longer.

def lots_of_math (a,b,c,d):
first=a+b
print(first)
second=c-d
print(second)
third=first*second
print(third)
fourth=third%a
return fourth

I am puzzled why do we need to return the value of fourth instead of using print?? Thank you very much for answers in advance.

I wrote it like this:

def first_three_multiples (num):

first_three_multiples = [num, num2, num 3]
return first_three_multiples

print (first_three_multiples(10))
print (first_three_multiples(0))

I made it into a list, when I checked the answer it was wrong and said multiples of 5 needed to print 5, 10, 15 when 5 was not mentioned in the question at all. Ridiculous.

This works for me

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

1 Like