开发者

syntax error due to several top-level functions in Python?

开发者 https://www.devze.com 2023-04-12 04:24 出处:网络
I searched and found a very similar question hereSyntax Error when trying to define multiple functions in python?

I searched and found a very similar question here Syntax Error when trying to define multiple functions in python?

but the question wasn't really answered

I am getting a syntax error at the second "def" . My indentation may be messed up here because I cannot figure out the formatting in this place....BUT it is correct in my program, indentation is not the issue.

def encryptChar(ch, key):
    newOrd = ord(ch) + key % 26
    if ord(ch) < 65:
        newOrd = ord(ch)
    elif ord(ch) < 91:
        if newOrd > 90:
            newOrd = newOrd - 26
        elif newOrd > 122:
            newOrd = newOrd - 26
    newch = chr(newOrd)
    return newch

def encryptString(string, key):
    newString = ""
    for i in range(len(string)):
         newString += encryptChar(string[i], key)
    return newString

def decryptString(string, key):
    newString = ""
    for i in range(len(string)):
         newString += encryptChar(string[i], -1*key)
    return newString

I'm GUESSING it has something to do with the functions all being top level. But how do I fix it? This is Python 2.7. (I could include the top function inside the other 2, but I am still left with 2 top level)

Thanks f开发者_JS百科or any help!


I was able to run your code with no problems once I fixed the indentation error:

def encryptString(string, key):
   newString = ""
   for i in range(len(string)):
        newString += encryptChar(string[i], key)
   return newString    # << HERE

Demo: http://codepad.org/eaaiHUJZ

I would also rewrite your code as follows:

# because encryption is misnomer here
def rotateChar(ch, key):
    if 'a' <= ch < 'z':
        newOrd = (ord(ch) - ord('a') + key) % 26
        return chr(newOrd + ord('a'))
    elif 'A' < ch < 'Z':
        newOrd = (ord(ch) - ord('A') + key) % 26
        return chr(newOrd + ord('A'))
    else:
        return ch

def rotateString(st, key):
   return ''.join(rotateChar(c, key) for c in st)

def derotateString(st, key):
   return ''.join(rotateChar(c, -key) for c in st)

Demo: Python 3, Python 2

Finally, I will strongly advise against using this kind of "encryption" for anything that requires real security.


The error message says exactly what's wrong:

  File "test.py", line 17
    return newString
                   ^
IndentationError: unindent does not match any outer indentation level

On the line return newString, the indentation level does not match the previous indentation level of the for i in range(len(string)) line.

You need to fix your indentation on the whole—use a constant indentation level (e.g. 2 or 4 spaces) throughout all your code. Be consistent. Right now, your indentation is all over the place—every time you indent, it's to a different level.

Secondly, don't use the name string for a variable. string is also the name of a standard Python module, so you will be unable to use that module with your code, and you will confuse other people reading your code (also don't use str, since that's the name of a built-in function).


I didn't get a syntax error at the start of your function, but at the end of it...

def encryptString(string, key):
   newString = ""
   for i in range(len(string)):
        newString += encryptChar(string[i], key)
    return newString 

Your return statement is indented one space further than the rest of the function, causing an IndentationError.

If you're sure the error is at the beginning of the code, there's another possibility. Stack Overflow automatically converts tabs to spaces in the code you post. If you were mixing tabs and spaces, you may have created an indentation error that is invisible to you. If you don't want to try to fix it manually, most text editors will have a feature to replaces tabs with spaces for you, or you can just copy the code from your post back into your program.


The problem is that return newString has an indentation level of 4 characters which does not match the earlier line at that level, which was at 3 characters. Try using an editor that makes it easy for you to always type 4-space tabs; writing Python will be very painful if you have to line everything up by hand and keep running into errors like this.


I don't know if it is a copy and paster error, but your indentation is kind of messed up. Try using mine and see if you have the same problem:

def encryptChar(ch, key):
    newOrd = ord(ch) + key % 26
    if ord(ch) < 65:
        newOrd = ord(ch)
    elif ord(ch) < 91:
        if newOrd > 90:
            newOrd = newOrd - 26
    elif newOrd > 122:
        newOrd = newOrd - 26
    newch = chr(newOrd)
    return newch

def encryptString(string, key):
    newString = ""
    for i in range(len(string)):
        newString += encryptChar(string[i], key)
    return newString

def decryptString(string, key):
    newString = ""
    for i in range(len(string)):
        newString += encryptChar(string[i], -1*key)
    return newString


What is the text of the error you're getting? Your function names shouldn't be clashing. My guess is that the extra space before return is giving you problems, but it would help to see the error.

0

精彩评论

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

关注公众号