Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Conditional checking without if

I’ve been checking out how to do the below question without an if else condition, spent almost a day. Is it possible to do this without an if else?

Check if area is greater than or equal to 15, then print "big place!" or if area is greater than 10 but less than 15, then print "medium size, nice!" or print "pretty small".

I can think of a while and breaking it after first print for now.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

or construction with logical expression, either print it or assign to new variable:

area = 15
print("big place!" * (area >= 15) or "medium size, nice!" * (area > 10) or "pretty small")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading