开发者

How to deal with IndentationError? [closed]

开发者 https://www.devze.com 2023-01-18 22:53 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcann开发者_如何学运维ot be reasonably answered in its current for
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cann开发者_如何学运维ot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

I get the following error:

File "imp.py", line 55
    key = get Key()
      ^
IndentationError: expected an indented block

With the following Code:

# Caesar Cipher

MAX_KEY_SIZE = 26

def getMode():
    while True:
        print('Do you wish to encrypt or decrypt or brute force a message?')
        mode = raw_input().lower()
        if mode in 'encrypt e decrypt d brute b'.split():
            return mode
        else:
            print('Enter either "encrypt" or "e" or "decrypt" or "d" or "brute" or "b".')

def getMessage():
    print('Enter your message:')
    return raw_input()

def getKey():
    key = 0
    while True:
        print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
        key = int(raw_input())
        if (key >= 1 and key <= MAX_KEY_SIZE):
            return key

def getTranslatedMessage(mode, message, key):
    if mode[0] == 'd':
        key = -key
    translated = ''

    for symbol in message:
        if symbol.isalpha():
            num = ord(symbol)
            num += key

            if symbol.isupper():
                if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                    num += 26
            elif symbol.islower():
                if num > ord('z'):
                    num -= 26
                elif num < ord('a'):
                    num += 26

            translated += chr(num)
        else:
            translated += symbol
    return translated

mode = getMode()
message = getMessage()
if mode[0] != 'b':
key = getKey()
print('Your translated text is:')
if mode[0] != 'b':
print(getTranslatedMessage(mode, message, key))
else: 
    for key in range(1, MAX_KEY_SIZE + 1): 
        print(key, getTranslatedMessage('decrypt', message, key))

How can I fix this?


  1. Do not use += to build strings. Use ''.join(mylist)
  2. Do as it asks: give it an indented block.


From An Informal Introduction to Python, "The body of the loop is indented: indentation is Python’s way of grouping statements."

If you are familiar with C or Java, you might recognize this syntax:

if (...)
{
    //do something
}

Python does this w/ indentations:

if ...
    #do something

That said, the rest of your code seems to understand this point. That you were unable to recognize this when the error occurred means either you got very lucky or you're using someone else's code.

I hope, for your sake, that this isn't a homework assignment, because most universities take a very dim view of plagiarism.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号