There are two main issues with your code. The first in that your return statement is executed in the first iteration of your loop, but you want it to happen after the entire loop has finished. This happens because you have too much whitespace to the left of the return statement. The second is that you’re attempting to return the return value of a call to print
. To fix this ditch the return
or even better return a string then print that:
numbers = (2,3,4)
def product(n):
m = 1
for i in n:
m *= i
return f"{n[0]}x{n[1]}x{n[2]}={m}"
returned = product(numbers)
print(returned)
The answer linked in the comments points out there’s an even easier way to do this:
from math import prod
returned = prod((2,3,4))
print(returned)
And here's a bit of a kick-flip for fun:
from math import prod
numbers = (2,3,4)
print(*numbers, sep="x", end=f"={prod(numbers)}")