[Tutor] If statement The Python Tutorial 3.4.1
ALAN GAULD
alan.gauld at btinternet.com
Sun Sep 14 10:00:49 CEST 2014
OK, You are using IDLE and have discovered one of its "features"
which I consider a bug...
The alignment of code breaks in IDLE when there is no >>> in the line.
You need to ignore the >>> characters.
So, to IDLE, your code looks like
if x < 0:
print(...)
elif x == 0:
because IDLE doesn't see the >>>.
So it gives you the unindent error because it thinks elif is not aligned
with if, even though it looks that way to you...
To fix it you need to make it look like this on screen:
>>> if x < 0:
print(...)
elif x == 0:
print(....)
else:
print(....)
Which looks wrong to you and me but IDLE sees it as good.
And this is one of the rare cases where posting an image rather
than a cut n' paste of the code is actually useful, because it
shows that you are using IDLE...
HTH
Alan Gauld
Author of the Learn To Program website
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos
>________________________________
> From: Gregory Donaldson <gvdonald at gmail.com>
>To: Alan Gauld <alan.gauld at btinternet.com>
>Sent: Sunday, 14 September 2014, 4:28
>Subject: Re: [Tutor] If statement The Python Tutorial 3.4.1
>
>
>
>
>
>
>
>
>On Sat, Sep 6, 2014 at 7:49 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
>On 06/09/14 00:29, Gregory Donaldson wrote:
>>
>>
>>This is what it looks like when I try to get it to work.
>>>
>>>
>>>x = int(input("Please enter an integer: "))
>>>>>>
>>>Please enter an integer: 42
>>>
>>>
>>>if x < 0:
>>>>>>
>>>x = 0
>>>
>>>print('Negative changed to zero')
>>>
>> I'm guessing that you actually indented the two lines
>>above but not the one below - otherwise you'd get an
>>indentation error by now, not a syntax error below.
>>
>>
>>elif x == 0:
>>>
>>>SyntaxError: invalid syntax
>>>
>> Are you sure this is where you get the syntax error?
>>Or did you maybe hit enter too many times thus ending
>>the if block?
>>
>>
>>It needs to follow this pattern:
>>
>>if x < 0:
>> x = 0
>> print('Negative changed to zero')
>>elif x == 0:
>> print('you typed zero')
>>else:
>> print('alls well')
>>
>>So the if/elif and else lines are all lined up with each
>>other and the indented lines are likewise lined up.
>>And if working in the interactive prompt you must not
>>have blank lines (Note: You can have blanks when creating
>>a program file)
>>
>>Indentation is always important in programming for readability,
>>but in Python its vital for Python to understand your code
>>correctly.
>>
>>HTH
>>--
>>Alan G
>>Author of the Learn to Program web site
>>http://www.alan-g.me.uk/
>>http://www.flickr.com/photos/alangauldphotos
>>
>>
>>_______________________________________________
>>Tutor maillist - Tutor at python.org
>>To unsubscribe or change subscription options:
>>https://mail.python.org/mailman/listinfo/tutor
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140914/87b96aa0/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot 2014-09-13 23.27.15.png
Type: image/png
Size: 44029 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20140914/87b96aa0/attachment-0001.png>
More information about the Tutor
mailing list