[Tutor] if/else statement

Alan Gauld alan.gauld at yahoo.co.uk
Tue Jul 18 19:10:30 EDT 2017


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




More information about the Tutor mailing list