简体   繁体   中英

Indentation problems! (Python 2.7)

What's wrong with this? (And I'm only a beginner, so nothing complicated, please!)

#!usr/bin/python

import os
import sys
import pdb
import time
import array

red = chr(00), chr(00), chr(255)
blue = chr(255), chr(00), chr(00)

print "Monochrome Bitmap Color Changer"
print "Supported colors: "
print "Black, White, Blue, Red, Yellow,"
print "Green, Orange, Purple, Pink, Brown, Grey"
print ""

filename = raw_input("Please enter filename or directory of monochrome bitmap: ")
whitevalue = raw_input("Change all white pixels to? ")
blackvalue = raw_input("Change all black pixels to? ")
with open (filename, 'r+b') as f:
    f.seek(54)
    if whitevalue is "red" or "Red":
        f.write(red)
    elif whitevalue is "blue" or "Blue":
            f.write(blue)
            f.seek(58)
            if blackvalue is "red" or "Red":
        f.write(red)
        elif blackvalue is "blue" or "Blue":
            f.write(blue)
            exit
    #print "Done"
    #time.sleep(3)
    #exit

After the second f.write(red) line, it shows a reddish-pinkish highlight and says:

'unindent does not match any outer indentation level'

What does this mean, and how can help/fix it?

Thank you very much!

That elif statement isn't in the same scope as an if statement so it will give you an error. Elif statements can only exist if there is already an if statement in the same scope/indentation.

If you want it to be an elif statement in line with

if blackvalue is "red" or "Red":

then indent it one more tab over. If you want to make it in the same scope as

if whitevalue is "red" or "Red":

and

elif whitevalue is "blue" or "Blue":

then un-tab in once. But to summarize, you can't have an elif statement by itself (just like you can't have an else statement by itself).

Your elif isn't indented a way that it belongs to an if statement.

Change it like this:

with open (filename, 'r+b') as f:
    f.seek(54)
    if whitevalue is "red" or "Red":
        f.write(red)
    elif whitevalue is "blue" or "Blue":
        f.write(blue)
        f.seek(58)
        if blackvalue is "red" or "Red":
            f.write(red)
        elif blackvalue is "blue" or "Blue":
            f.write(blue)
        exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM