newbie syntax question for Python - indenting

Gadsden Consulting
Gadsden Consulting used Ask the Experts™
on
Looking at Python for the first time … going through these tutorials

I'm running Python via SPSS Statistics (Python 3.4.3 Shell) and got this error which is entered directly from the tutorial:
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 55
>>> if x < 0:
	x = 0
	print('Negative changed to zero')
	elif x == 0:
		
SyntaxError: invalid syntax
>>> 

Open in new window


In Anaconda I get this error:
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 55
>>> if x < 0:
... x = 0
  File "<stdin>", line 2
    x = 0
    ^
IndentationError: expected an indented block
>>>

Open in new window

And if I press the Indent key, Anancoda throws up on me ...

So not sure what I'm doing wrong right out of the gate ...

Also, is there a recommended IDE for Python?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
I think the elif statement needs to go to the left.  On the command line, indentations are tricky to get right.

I would use a text editor (Notepad++, Notepad) or IDE (VS Code, Spyder, PyCharm, etc.) to create a .py file and then invoke the .py file from the command line.

I'm not sure why x = 0 is being flagged as invalid.

It is preferred to use spaces for indentation, rather than tabs.  Hopefully, your IDE or text editor will make this easier than the command line.
Gadsden ConsultingIT Specialist

Author

Commented:
aikimark,

thanks! Yes, I figured out about spacing and not tabs … but thanks, especially for the coding/IDE tips.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial