+ 2

Need help with the gold purity Boolean logic problem

I'm running this code and it's showing errors on test 5 purity = float(input()) if purity >= 75.0 and purity < 83.3: print ("18K") if purity >= 83.3 and purity < 91.7: print ("20K") if purity >= 91.7 and purity < 99.9: print ("22K") if purity >= 99: print ("24K") Any help would be appreciated 🙂

6th Aug 2021, 1:18 PM
Tophyy
Tophyy - avatar
6 Respostas
+ 5
Try: purity = float(input()) if purity >= 75.0 and purity < 83.3: print ("18K") elif purity >= 83.3 and purity < 91.7: print ("20K") elif purity >= 91.7 and purity < 99.9: print ("22K") elif purity >= 99.9: print ("24K")
6th Aug 2021, 1:28 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Could you post the task here?
6th Aug 2021, 1:21 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Thank you that worked. What was I doing wrong then 😅
6th Aug 2021, 1:31 PM
Tophyy
Tophyy - avatar
+ 1
1. use elif so statements are only considered if original if is not the case. 2. You wrote >= 99 instead of 99.9 in the last statement
6th Aug 2021, 1:32 PM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 1
Oh thank you I didn't see the 99.9 part and I'll remember to use elif also thanks.
6th Aug 2021, 1:34 PM
Tophyy
Tophyy - avatar
- 1
Now that we know how to combine multiple conditions, let’s improve our gold purity checker program and output the corresponding purity level in Karats! Here is the purity chart we’ll be using: 24K – 99.9% 22K – 91.7% 20K – 83.3% 18K – 75.0% If the percentage is between 75 and 83.3, the gold is considered to be 18K. If it's between 83.3 and 91.7 - then it’s 20K, and so on. Given the percentage as input, output the corresponding Karat value, including the letter K. Sample Input: 92.4 Sample Output: 22K
6th Aug 2021, 1:22 PM
Tophyy
Tophyy - avatar