-2

I am trying to learn Python. I am running into this error. I the Python IDLE3.7.4 i type the following:

If name == 'Mart': print ('Hi Mary') else: print ('Wrong name')

This is the error:

if name == 'mary': print ('hi mary') else:

SyntaxError: unindent does not match any outer indentation level

The 'else' is highlighted as the error.

Can somebody explain why i have the problem. Thank you.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
tibi
  • 1
  • Does this answer your question? [IndentationError: unindent does not match any outer indentation level](https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level) – esqew Jul 13 '22 at 02:11
  • Welcome to Stack Overflow. Please read [ask] and the [formatting help](https://stackoverflow.com/help/formatting) and make sure that any code you post has correct formatting and appears **exactly** as you actually have it, as indentation is crucial in Python. The best way to do this is to **copy and paste** the code, then put code fences around it - three backticks *on a line by themselves, both before and after* the code. The code shown here clearly does not correspond to the error message - keep in mind that Python is case sensitive (please be careful of your autocorrect). – Karl Knechtel Jul 13 '22 at 02:13
  • You need to have some sort of statement after the ```else:```. You can't leave the else block empty. If you don't have a statement for the else block, just either remove the ```else``` block or add a ```pass``` statement – ewokx Jul 13 '22 at 02:13
  • 1
    For those who attempted to edit the question to fix formatting: I have rolled the changes back, because it is not actually clear how OP has the code indented, whether the actual code includes any particular typo, etc. It is OP's responsibility to fix the problem at this point; `in the face of ambiguity, resist the temptation to guess`. – Karl Knechtel Jul 13 '22 at 02:16

2 Answers2

1

Try to use code blocks and separate the code into different lines. The issue you are having is with indentation. Python uses indentation as syntax to separate blocks of code. You will either want to only use tabs or only use spaces for your code. Here is your code fixed:

if name == 'Mart':
    print ('Hi Mary')
else:
    print ('Wrong name')

I used 4 spaces so adjust that accordingly. The issue is probably you mixing up spaces and tabs, go back through and make them all either spaces or tabs, no mixing.

Samisai
  • 73
  • 1
  • 5
-1

That because you wrote the code in the incorrect syntax.

Solution 1 (Longer):

if name == "mary":
    print("hi mary")
else:
    print("Wrong name")


Solution 2(Shorter version):

print("hi mary" if name == "mary" else "Wrong name")

Hope those help.

John
  • 21
  • 3