In this exercise, the first_three_multiples() function has three print statements. Can this be done in some other way?
Answer
Yes, there are several ways in which you could condense the code. A straightforward way would be the use of a for loop as shown below. Rember that the range() function does not include the end number so the range produced by this code would be 1 … 3.
def first_three_multiples(num):
for x in range(1, 4):
print(num * x)
return num*3
Expanding on this idea, you could also generalize the function to accept the multiple as a parameter so it would work for values other than 3.
def multiples(num, multiple):
for x in range(1, multiple + 1):
print(num * x)
return num * multiple
There is some confusion in these answers between print() and return.
return <expression> is a statement that directs the Python interpreter to:
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.
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
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
I agree the easiest to understand.
I had written:
"
def first_three_multiples(num):
first = num1
second = num2
third = num*3
print(first,second,third)
return third
"
Which is far longer.
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.