[Tutor] Conditional Statement without if statement?

Alan Gauld alan.gauld at yahoo.co.uk
Tue Feb 8 05:35:09 EST 2022


On 08/02/2022 03:20, Barrett Ferguson wrote:
> Hello,
> 
>  I have an assignment which asks me to create an if statement without using if, pls help. :)
> I have attached the complete question
> 
> Write code that sets the value of a variable named delicious to True if jellybeans is greater than 20, or licorice is greater than 30, or their sum is greater than 40. Otherwise, set it to False. Do NOT use an if statement in your solution. Assume jellybeans and licorice already have values.

I really hate it when teachers set homework that encourages
writing bad code!  What they really want you to do is use logical
operations. But they didn't want to say that because, presumably,
it was too much of a clue! So instead they create the impression that
you can/should use alternatives to the built in features of the
language! grrrrr!

But if you simply treat it as a logical operation then you can
just write it as given.

The only thing to remember after that is that Python returns
the actual values from a boolean comparison.

Thus

x = 42 or 66

x will have the value 66

To get that as a genuine boolean value (ie True or false) you
need to call bool()

-- 
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