[Tutor] if/else statement
Mats Wichmann
mats at wichmann.us
Tue Jul 18 20:11:48 EDT 2017
And one more, the 'if' needs a colon at the end
On July 18, 2017 5:10:30 PM MDT, Alan Gauld via Tutor <tutor at python.org> wrote:
>On 18/07/17 18:31, Shane Johnson (shanejoh) wrote:
>
>> def greater_less_equal_5(answer):
>> if answer is '>' 5
>> return 1
>> elif answer is < 5:
>> return 0
>> else:
>> return 4
>
>> I’m getting a invalid syntax line 2 error. Any assistance is greatly
>appreciated.
>
>Thee are two problems.
>
>1) 'is' is a problem, you don't need it here. 'is' is an operator
>for testing whether two object references are to the same
>actual object
>eg.
>
>x = 42
>y = x # y and x both refer to the same number
>if x is y: print 'yes!'
>
>You don't use it in mathematical comparisons so your code
>should look like:
>
> if answer > 5
> return 1
> elif answer < 5:
> return 0
> else:
> return 4
>
>Notice I also removed the quotes around the > sign and
>added indentation to the return statements which leads
>us to...
>
>2) You don't have any indentation in the function body.
>Indentation is all important in Python, it's how the
>interpreter knows where the conditional block starts
>and stops.
>
>HTH
>--
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>http://www.amazon.com/author/alan_gauld
>Follow my photo-blog on Flickr at:
>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
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
More information about the Tutor
mailing list