Indentation error when running python code in VS Code
Question:
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
When I run the above python program, Visual Studio Code gives me the following error:
[Running] python -u "e:College WorkHND Cyber
SecurityProgrammingPythonBlock 1Algorithms and ProgramsUnfinished
Work (Program and Documentation to finish)Ceaser Cipher Program
(program and documentation to do)Caeser Shift Program.py"
File "e:College WorkHND Cyber SecurityProgrammingPythonBlock
1Algorithms and ProgramsUnfinished Work (Program and Documentation
to finish)Ceaser Cipher Program (program and documentation to
do)Caeser Shift Program.py"
line 15
return input()
^ TabError: inconsistent use of tabs and spaces in indentation
[Done] exited with code=1 in 0.145 seconds
I have tried changing the indentation a few times with no luck. Can anyone explain what the issue could be and what I could do to solve it?
Answers:
This is a very common problem. You need to use 4 white spaces
instead of using a tab
. But make sure you are consistent and follow this style everywhere. Similarly, you can use tabs
all the way but do not mix white spaces
with the tabs
as stated in the comment by Error – Syntactical Remorse.
The code needs to be consistent in the usage of tabs and spaces. The entire code can use 4 white spaces or the entire code can use tab, but not a mix of both.
In Visual Studio Code do CTRL
+ SHIFT
+ P
and type in “indentation”. You will see options for converting indentation: Convert Indentation to Spaces
or Convert Indentation to Tabs
. Select one and stick with your chosen indentation.
Yeah A few things weren’t indented correctly like the last return translated
I fixed it you can diff check for the difference, python doesn’t like it when you are inconsistent with indentations
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
This should fix it:
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(input())
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode, message, key):
translated = ''
num = 0
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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
Also the variables num
and translated
were used before they were initialized. I have fixed those too.
Forgetting a semicolon at the end of the function definition produces the same error.
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
When I run the above python program, Visual Studio Code gives me the following error:
[Running] python -u "e:College WorkHND Cyber
SecurityProgrammingPythonBlock 1Algorithms and ProgramsUnfinished
Work (Program and Documentation to finish)Ceaser Cipher Program
(program and documentation to do)Caeser Shift Program.py"File "e:College WorkHND Cyber SecurityProgrammingPythonBlock
1Algorithms and ProgramsUnfinished Work (Program and Documentation
to finish)Ceaser Cipher Program (program and documentation to
do)Caeser Shift Program.py"line 15
return input()
^ TabError: inconsistent use of tabs and spaces in indentation[Done] exited with code=1 in 0.145 seconds
I have tried changing the indentation a few times with no luck. Can anyone explain what the issue could be and what I could do to solve it?
This is a very common problem. You need to use 4 white spaces
instead of using a tab
. But make sure you are consistent and follow this style everywhere. Similarly, you can use tabs
all the way but do not mix white spaces
with the tabs
as stated in the comment by Error – Syntactical Remorse.
The code needs to be consistent in the usage of tabs and spaces. The entire code can use 4 white spaces or the entire code can use tab, but not a mix of both.
In Visual Studio Code do CTRL
+ SHIFT
+ P
and type in “indentation”. You will see options for converting indentation: Convert Indentation to Spaces
or Convert Indentation to Tabs
. Select one and stick with your chosen indentation.
Yeah A few things weren’t indented correctly like the last return translated
I fixed it you can diff check for the difference, python doesn’t like it when you are inconsistent with indentations
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
This should fix it:
MAX_KEY_SIZE = 26
def getMode():
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')
def getMessage():
print('Enter your message:')
return input()
def getKey():
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(input())
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(mode, message, key):
translated = ''
num = 0
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()
key = getKey()
print('Your translated text is:')
print(getTranslatedMessage(mode, message, key))
Also the variables num
and translated
were used before they were initialized. I have fixed those too.
Forgetting a semicolon at the end of the function definition produces the same error.