-1

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 for any help!

3
  • What is the text of the error message you're getting? Commented Oct 12, 2011 at 3:47
  • I don't get any errors. What exactly are you getting? Commented Oct 12, 2011 at 3:48
  • It's not an indentation problem. If it was you would get an IndentationError, not a SyntaxError. Nobody has been able to reproduce your problem, so triple check that you are actually using the code you posted here. Commented Oct 12, 2011 at 4:24

6 Answers 6

3

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.

Sign up to request clarification or add additional context in comments.

11 Comments

The new ...String functions are of course list comprehensions for the uninitiated.
oh man, the indentation was all messed up because I didn't know how to post the code in the message box... I STILL get the error, with the "fixed" indentations. the error is "SyntaxError: invalid syntax" and it highlights the second "def". thx for the other suggestions!
@user990678 Have you try my demos?
@user990678 It works fine on Python3 as well. Can you copy and paste the entire error?
"SyntaxError: invalid syntax" is the entire error. and it highlights the second "def"
|
2

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

Comments

1

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.

Comments

1

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.

Comments

1

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

1 Comment

oh man, the indentation was all messed up because I didn't know how to post the code in the message box... I STILL get the error, with the "fixed" indentations. the error is "SyntaxError: invalid syntax" and it highlights the second "def".
0

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.

1 Comment

oh man, the indentation was all messed up because I didn't know how to post the code in the message box... I STILL get the error, with the "fixed" indentations. the error is "SyntaxError: invalid syntax" and it highlights the second "def".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.