How does indentation need to match up?

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.

2 Likes

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