24K | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1


Hi everyone, For the purity checker, is there an easier way to do this? The code works but I feel like I made it bulkier than necessary? Could I have used elif in this? purity = float(input()) if(purity >= 75.0 and purity <= 83.2): print('18K') if(purity >= 83.3 and purity <= 91.6): print('20K') if(purity >= 91.7 and purity <= 99.8): print('22K') if(purity >= 99.9): print('24K')

5th Aug 2021, 10:55 PM
3 Answers
+ 5
This is a little more compact and should do it. The number of lines is the same, but the if statements are shorter. https://code.sololearn.com/cbMblg19s7Ln/?ref=app
5th Aug 2021, 11:26 PM
Simon Sauter
Simon Sauter - avatar
+ 2
I see where my thought process went now. For lack of a better term, I was establishing the max and minimum twice instead of letting the prior if statement set it. Thanks for sharing that.
5th Aug 2021, 11:54 PM
+ 2
If you want to optimize, you need to avoid to go through all if statements each time. If else statements or return/ break in ifs should help
6th Aug 2021, 12:30 AM
Gabriela G
Gabriela G - avatar