FAQ: Code Challenge: Python Functions - All Operations

Thank you! That makes sense!

1 Like

Could you tell me why this causes an error:

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

I don’t really see the problem here but I do get the error message:

“Traceback (most recent call last):
File “script.py”, line 9, in
print(lots_of_math(1, 2, 3, 4))
File “script.py”, line 6, in lots_of_math
third = print (first * second)
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘NoneType’”

First first: To post code, please use the </> icon in the menu bar that appears at the top of the text box when you open it to type.

Doesn’t this look better?

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

“Traceback (most recent call last):
File “script.py”, line 9, in 
print(lots_of_math(1, 2, 3, 4))
File “script.py”, line 6, in lots_of_math
third = print (first * second)
TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘NoneType’”

Second “first”: Your variables first and second are the return values from print(), which is a function that does the following:

  1. Evaluates the expression within its parentheses
  2. Sends the resulting value as a string to the default i/o device, usually your screen
  3. Returns the value None.

In other words, print() prints a value, but returns None, so when you code
first = print (a + b)

print() evaluates the expression a + b, prints that value to the screen, and then returns None, which is the value assigned to the variable first. Likewise with second. You cannot do None * None, hence the error message

TypeError: unsupported operand type(s) for *: ‘NoneType’ and ‘NoneType’”

I’m puzzled. The browser lets me pass with the following code (very similar to those posted above):

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

But this code returns only positive numbers:
3
1
3
0
2
0
0
0

Unlike what is expected according to the comments: 3, -1, -3, 0 and 2, 0, 0, 0.

What should I do different? Or are the instructions wrong?

3 + -1  ==  2  (first)

0 - -3  ==  3  (second)  ##

2 * 3   ==  6  (third)

6 % 3   ==  0  (fourth)

## Remember that `minus minus` is `plus`.
1 Like

Thanks, mtf. I’m aware of the basic math rules. And that’s why I was puzzled: I just couldn’t figure out how my code produced different answers if it was essentially the same as everyone elses, and the browser let me pass (green light). I now noticed, however, that I had inverted the order between d and c in the second variable (should be c minus d, and not d minus c). So problem solved anyway :slight_smile:

Your replies in the forum have been very helpful over several Codecademy courses, thanks for taking the time!

2 Likes

general question:

Can someone explain the difference between using the “print()” within your functions rather than just returning all the values?

For example using the code:

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

will give you the same answers but the will be formatted differently. Is there any advantage of using print vs. returning the values and when would you want to use one over the other?

@jacobgarwin295317990, use print() when you want to see the value on the screen.

Use return when Python needs to make use of the value, say, to assign to a variable, to send to another function as an argument, or to use as a value in an expression.

1 Like

Finally, it should return the third number printed mod a, this is a poorly written step, whoever wrote this exercise, please, get down to our level, I know you are an expert, but this is the first time I learned about this word “mod” , this exercise is confusing! you can do a better job

1 Like