pythonfunctionmultiplication

Creating a multiplying function


I don't understand how to make a function and then make it work which will allow me to multiply. For E.g.

def Multiply(answer):
    num1,num2 = int(2),int(3) 
    answer = num1 * num2
    return answer

print(Multiply(answer))

I Had a go at making one and didnt work so the one below is where i found on internet but i have no idea how to make it work as in print out the numbers timed.

def multiply( alist ):
     theproduct = 1
     for num in alist: theproduct *= num
     return theproduct

Solution

  • I believe you have your parameter as your return value and you want your paramters to be inputs to your function. So try

    def Multiply(num1, num2):
        answer = num1 * num2
        return answer
    
    print(Multiply(2, 3))
    

    As for the second script, it looks fine to me. You can just print the answer to the console, like so (notice it takes a list as an argument)

    print multiply([2, 3])
    

    Just know that the second script will multiply numbers in the list cumulatively.