-4

I've created a variable that adds two numbers together than another which multiplies that number by 2. After these two functions I try to then divide the result of these two functions by two. It will not work. Is it because I did not make the result of the previous function a global variable?

I've tried making it a global var but it doesn't work.

def add(num1,num2):
    return num1 + num2

def multiply():
    mult = add(1,2)*2
    print(mult)

def divide():
    start = multiply() /2
    print(start)

divide()

I expected the result 3 but instead it threw up an error message instead. What did I do wrong?

vurmux
  • 9,420
  • 3
  • 25
  • 45
zman
  • 51
  • 1
  • 7

1 Answers1

2

Your multiply() doesn't return anything so when the divide() starts to work, it tries to divide None to 2 and raises an error. Here is a bit fixed code:

def add(num1,num2):
    return num1 + num2

def multiply():
    mult = add(1,2)*2
    return mult

def divide():
    start = multiply() /2
    return start

print(divide())

It will print:

3.0


But here is deeper problem:

Your multiply and divide functions have no inputs so they are useless and multiply function and divide function. You can rewrite them as:

def add(num1,num2):
    return num1 + num2

def multiply(num1,num2):
    return num1 * num2

def divide(num1,num2):
    return num1 / num2

print(divide(add(1, 3), multiply(2, 5)))

0.4

vurmux
  • 9,420
  • 3
  • 25
  • 45