3

I'm brand new to Python, and I was wondering if someone could help me with something that seems to be a simple fix. However, I can't figure it out.

When I copy and paste the following code from my text editor to my terminal, I get an error saying:

IndentationError: unexpected indent

When I manually type in everything in Terminal, I don't get the error message and the program runs perfectly fine.

def decrypt(key, txtFile): \
    txtFile = encrypted.upper() \
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
    result = "" \
    for char in txtFile: \
        if char in alpha: \
            char_index = (alpha.find(char) - key) % len(alpha) \
            result = result + alpha[char_index] \
        else: \
            result = result + char \
    return result

'''

Kane
  • 91
  • 7
  • 2
    Why do you have all those backslashes? – user2357112 Mar 02 '20 at 00:58
  • Stop copy-pasting from file to terminal. If it's Python code in a file, run the file directly. – Gino Mempin Mar 02 '20 at 04:28
  • Does this answer your question? [What to do with "Unexpected indent" in python?](https://stackoverflow.com/questions/1016814/what-to-do-with-unexpected-indent-in-python) – Gino Mempin Mar 02 '20 at 04:33
  • If you use [IPython](https://ipython.org/) instead of the regular Python console, you can paste entire code snippets with blank lines into a terminal without worrying about indentation errors. – Zenul_Abidin May 22 '23 at 06:39

2 Answers2

1

Python is all about indentation and whitespace in functions and classes.

If the above is the pasted code, you have the '\' on every line which may be causing the error. If this is still what is typed in and still not running - the white space indent from column 0 to 4 (1 tab space) may be another encoding so therefore causing an issue. Try copying the function and then removing the indentation then adding it back yourself, after you have removed the '\' from every line.

def decrypt(key, txtFile): 
    txtFile = encrypted.upper() 
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
    result = "" 
    for char in txtFile: 
        if char in alpha: 
            char_index = (alpha.find(char) - key) % len(alpha) 
            result = result + alpha[char_index] 
        else:
            result = result + char
    return result
Hamodey_
  • 66
  • 10
  • Also try using nano or vim when writing in terminal or go to your IDE. – Hamodey_ Mar 02 '20 at 01:02
  • 2
    Thank you so much! You and the other guy helped me out a bunch. I'm not sure what I did wrong. I added the \ after trying a bunch of other stuff including rewriting it several times. – Kane Mar 02 '20 at 01:04
0

It looks like your terminal is adding a backslash to the end of each line when it doesn't need it here. The backslash tells the interpreter to treat the two lines as a single line, and we don't want that here. Removing the \ worked for me.

def decrypt(key, txtFile):
    txtFile = encrypted.upper()
    alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = ""
    for char in txtFile:
        if char in alpha:
            char_index = (alpha.find(char) - key) % len(alpha)
            result = result + alpha[char_index]
        else:
            result = result + char
    return result
Rusty Robot
  • 1,725
  • 2
  • 13
  • 29
  • 1
    Thank you very much for your help. I wasn't sure what \ did. I just noticed that some of the source code that my professor provided us had them for lines that were to be treated as one, but I wasn't sure exactly what to do. – Kane Mar 02 '20 at 01:05