0

When I run the code, I get this compile error:

IndentationError: unindent does not match any outer indentation level

I think the error occurs because of the return in the last line. What should I do differently?

def whileexample(): n=15;i=0; # Mit Semikolon = Variablen in einer Zeile schreiben while i<=n: if n>20: print n, "ist zu groß" break print "%d : %7d" % (i,2**i) i=i+1 else: print n+1, "Zweierpotenzen berechnet." return whileexample() 
3
  • Can you be more precise about what confuses you in the print statement? Commented Oct 14, 2016 at 0:16
  • I am German nationility. Sorry, because of my writing skills. Commented Oct 14, 2016 at 0:21
  • The bold text confuses me: print "%d : %7d" % (i,2**i) Commented Oct 14, 2016 at 0:23

5 Answers 5

1

@Robᵩ is correct with the whitespacing. As for your other question, %d and %7d are place holders for whatever is in the parentheses.

The 'd' in this case means you want whatever is displayed in the parentheses to be formatted as a decimal.

The '7' indicates how much whitespace before the next variable.

The 2**i means 2 raised to the i (2^i).

Ex:

>>> print "%d : %7d" % (5, 2**5) 5 : 32 
Sign up to request clarification or add additional context in comments.

3 Comments

And what means the 7? And what means the % before (5, 2**5)
Sorry, I mean "what means the % before (5, 2**5) "
The % before (5, 2**5) tells the string "%d : %7d" that you want to use the values in the parentheses to fill the placeholder positions.
1

IndentationError: unindent does not match any outer indentation level

The Indentation Error is on the last line where the function is being called. whileexample() in the last line has an extra space in the front.That should not be there. It has to be at the same indentation level as the def statement.

To the second question, print("%d : %7d" % (i,2i)), whatever is inside the quotes is the format to display whatever is inside the tuple (). Here, i is the first value displayed as %d which stands for decimal integer and 2i is the value displayed as %7d where 7 denotes the number of spaces between the colon and the value.

Comments

0

In Python, the whitespace at the beginning of the line is significant. Statements at the same logical level must be indented the same amount.

In your case, the final line has an extra space character at the beginning of the line. Make sure what the w in the last line is all the way to the let, in the very first column.

Comments

0

Your final piece of code: whileexample() You added a redundant space in this first column.

Comments

0

The problem that I found was that the last line was indented when it didn't need to be

if you are using Python 3 try this:

 def whileexample(): n=15;i=0; # Mit Semikolon = Variablen in einer Zeile schreiben while i<=n: if n>20: print (n, "ist zu groß") break print ("%d : %7d" % (i,2**i)) i=i+1 else: print (n+1, "Zweierpotenzen berechnet.") return whileexample() 

if using python2 try this;

def whileexample(): n=15;i=0; # Mit Semikolon = Variablen in einer Zeile schreiben while i<=n: if n>20: print n, "ist zu groß" break print "%d : %7d" % (i,2**i) i=i+1 else: print n+1, "Zweierpotenzen berechnet." return whileexample() 

The difference between these codes it that the Python 3 has parenthesis around the print as Python 3 needs this and Python 2 doesn't.

1 Comment

You can condense this into a single example that starts with from __future__ import print_function.