[Tutor] Conditional Statement without if statement?

Barrett Ferguson barrettf50cal at gmail.com
Tue Feb 8 20:17:37 EST 2022


Alright guys, I went to tutoring hours and got some help to figure out the answer. Il’ll re-enter the question for reference: 
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.

The answer is: delicious = jellybeans > 20 or licorice > 30 or jellybeans + licorice > 40

What are yalls thoughts?

> On Feb 8, 2022, at 18:50, Alan Gauld via Tutor <tutor at python.org> wrote:
> 
> On 08/02/2022 10:35, Alan Gauld via Tutor wrote:
> 
>> 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
> 
> Oops! And that demonstrates why you shouldn't use it
> as a conditional statement! :-)
> 
> I actually had a much longer answer typed out with
> multiple examples from the >>> prompt but then decided
> I was overcomplicating things so used this simpler but
> untested example. sigh.
> 
> -- 
> 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



More information about the Tutor mailing list