user7016465
user7016465

Reputation:

Python - Beginner questions

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()

Upvotes: -1

Views: 363

Answers (5)

Mnag
Mnag

Reputation: 116

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.

Upvotes: 0

Kirsty Douglas
Kirsty Douglas

Reputation: 33

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.

Upvotes: 0

AustinTronics
AustinTronics

Reputation: 11

@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

Upvotes: 1

Haohu Shen
Haohu Shen

Reputation: 58

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

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168626

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.

Upvotes: 0

Related Questions