FAQ: Code Challenge: Python Functions - All Operations

This community-built FAQ covers the “All Operations” exercise from the lesson “Code Challenge: Python Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Data Science

FAQs on the exercise All Operations

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

I’m in the Lots of Math part of Python Functions. It’s the last screen before you complete the lesson.

It is asking me to “return the third number printed mod a.” What exactly is this asking me to do?

10 Likes

I had the same question. Mod is short for modulo, which is the % thing.

5 Likes

I was a bit confused by this question. It says the function should print 3 lines and return 1 value - but in the “should print” part of the comments it shows all the values in one row (not just 1 value).

I did eventually get this right by just returning one value, but then it didn’t look like the suggested solution in the comments - so perhaps the phrasing was a bit odd. Or is it just me?

2 Likes

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

This is the code I used… is it correct? Not sure if I was supposed to do that. How do I get it all on one line?

Mod means a lot for some of us, can be modules, modifications, moderations so fix this exercise asap because its confusing.

5 Likes

I have a question about Code challenge (Lots of math) task.

The task is the following (among others): Finally, it should return the third number printed mod a.

If it should just return, not print the 4th value, why in the exercise solution there is 4th and 8th numbers present?
IMO, there should be just 3 values printed for the first function call, and 3 for the second.

Recall that the function has three print statements, and on return we print again. That last print will be mod a value, 0.

I have a Problem, if I use this code:

def lots_of_math(a, b, c, d):
		first = a + b
		second = c - d

print (first = a + b)
print (second = d - c)
print (first) * print (second)
return 


print(lots_of_math(1, 2, 3, 4))

I get this Error Message:

  File "script.py", line 9
    return 
    ^
IndentationError: unexpected indent

I tried it with different indentation levels, but the error message stays the same.
I don`t know what went wrong.

The first indentation following the declaration (the def line terminationg with a colon) sets the default indentation level for the entire function. Here, it appears to be eight spaces, or perhaps two tabs. Your function will (try to) run if every line is indented to the same level as the word ‘first’, and each line is using either spaces or tabs, whatever that ‘first’ line is using.

However, the default indentation for the Python editor used in these courses is two spaces. Whenever the editor encounters a colon, it will automatically indent the next line two spaces. In order to be consistent with the code used in the remainder of the course, and to get used to this particular editor, you should re-type the function, making sure to maintain that default level for each line.

When you do so, you will receive no more “indentation” errors, but will encounter a few others.

Thank`s for the reply, I will start over and try to use the default indentation.

>>> a = 6; b = 7
>>> print (first = a + b)
Traceback (most recent call last):
  File "<pyshell#102>", line 1, in <module>
    print (first = a + b)
TypeError: 'first' is an invalid keyword argument for this function
>>> 

Sorry,
It does not work with standard indentation levels.

# Write your lots_of_math function here:
def lots_of_math(a,b,c,d):
	first = a + b
	second = d - c
	third = first * second
  fourth = third % a
  print (first, second, third, fourth)
	
# Uncomment these function calls to test your lots_of_math function:
print(lots_of_math(1, 2, 3, 4))
# should print 3, -1, -3, 0
print(lots_of_math(1, 1, 1, 1))
# should print 2, 0, 0, 0

There`s something wrong with the system right now, the “fourth” and “print” is in my code on the same indentation level as the before mentioned ones!

debug message:

File "script.py", line 6
    fourth = third % a
                     ^
IndentationError: unindent does not match any outer indentation level
type or paste code here

I`m a little bit confused, because the indentation is the standard 1 time click on tabulator.
Or is the standard in this challenge one space and not one tabulator?

The standard is that it must be consistent throughout any block. You can’t mix tabs & spaces. The exact number of spaces (or tabs) is not critical, just the consistency with the first indentation following the colon, including the first “”" of a docstring.

Granted, that the editor here at CA seems hypercritical at times. I have more than once simply unindented an entire function and re-indented it line-by-line. The problems seem most likely if I am copying and pasting from one editor to another.

1 Like

Here is the result (i indentated everything again per hand, line by line) , no more indentation error!
But a new error has shown up, here is the code:

# Write your lots_of_math function here:
def lots_of_math(a,b,c,d):
	first = a + b
	second = c - d
	third = first * second
	fourth = third % a
	print (first, second, third, fourth)
  
# Uncomment these function calls to test your lots_of_math function:
print(lots_of_math(1, 2, 3, 4))
# should print 3, -1, -3, 0
print(lots_of_math(1, 1, 1, 1))
# should print 2, 0, 0, 0

and here is the error message:

3 -1 -3 0
None
2 0 0 0
None

lots_of_math(1, 2, 3, 4) returned None, expected 0.

Yes its the final code challenge, and i`m feeling like i am treading water.

From the instructions:

The function should print 3 lines and return 1 value.

Your function prints one line and returns the value None.

Remember:

  • We print() a value that we want to see on the screen
  • We return a value that Python can use, to assign to a variable, say, or pass to another function (such as a print() function.) If a returned value is not used, it is lost. In Python, if a function has no return statement, it returns the value None.
def print_and_return(x, y):
  print(x)
  return y

a = "I printed this"
b = "I returned this"
print_and_return(a, b)
print()
print(print_and_return(a, b))

# Output:
I printed this  # This is from print() The return value is lost

I printed this    # This is from print() 
I returned this     # This is the returned value printed by the calling statement

Done!

print (first, second, third)

and then return the fourth value

return fourth

and it has checked out.

Sometimes I feel very stupid…

P.S. Is it advisable to open a new topic in the forum under (for example) python 3 / get help to Post questions like these, because I can not “check” that your solutions have worked. I mean clicking on the square shaped button, to show everyone that the solution has been reached.

1 Like

yes it seems correct

Okay, can anyone tall me why it’s not necessary to use the str function in picture-link below? first, second, third, and fourth are all numbers, so wouldn’t it be necessary to use str when printing?

Btw, sorry if this is not posted on the right place on the web. The instructions I followed pointed me to this place :man_shrugging:

print() will take any expression, including an operation.

print (6 + 7)           # 13
print (6 * 7)           # 42

The operation depends upon matching types.

print ('a' + 'b')       # ab

We would only use str() in the event one operand is a string object, and the other, not.

print ('a' + 1)         # TypeError

print ('a' + str(1))    # a1