24K | Sololearn: Learn to code for FREE!

+1

24K

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

8/5/2021 10:55:14 PM

Mannsfate

3 Answers

New Answer

+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

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

+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