+ 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 đ
6 RĂ©ponses
+ 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")
+ 1
Could you post the task here?
+ 1
Thank you that worked. What was I doing wrong then đ
+ 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
+ 1
Oh thank you I didn't see the 99.9 part and I'll remember to use elif also thanks.
- 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