+ 10

SOLVED 24K magic exercise in python for beginners - HELP

The task is to have the karat displayed for an inputted purity of gold. If the purity is under 75%, nothing should display. I'm getting all of the test cases right except the last with the below code, what am I doing wrong? purity = float(input()) if purity >= 99.99 and purity <= 100: print("24K") elif purity >= 91.7: print("22K") elif purity >= 83.3: print("20K") elif purity >= 75.0: print("18K")

15th Feb 2021, 10:18 AM
Zoë
Zoë - avatar
27 Antworten
15th Feb 2021, 11:33 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 12
Remove the 'and' operator and the rest in your first condition. Everything will be fine. So, Your code needs to be purity = float(input()) if purity >= 99.99: print("24K") elif purity >= 91.7: print("22K") elif purity >= 83.3: print("20K") elif purity >= 75.0: print("18K")
15th Feb 2021, 12:08 PM
Simba
Simba - avatar
+ 6
🇮🇳 R😻 thanks for the responses. I tried that and it's still not working for the last test case. Wish I knew what the test case is!
15th Feb 2021, 11:53 AM
Zoë
Zoë - avatar
+ 5
Zoë did you wrote 99.9 or 99.99?
15th Feb 2021, 1:57 PM
Abhay
Abhay - avatar
+ 2
Abhay THANK YOU! That's exactly where I went wrong, all working now.
15th Feb 2021, 2:08 PM
Zoë
Zoë - avatar
+ 2
Zach Z I had to change 99.99 to 99.9.
22nd Mar 2021, 10:42 AM
Zoë
Zoë - avatar
+ 1
The code in the question is my attempt: purity = float(input()) if purity >= 99.99 and purity <= 100: print("24K") elif purity >= 91.7: print("22K") elif purity >= 83.3: print("20K") elif purity >= 75.0: print("18K")
15th Feb 2021, 10:26 AM
Zoë
Zoë - avatar
+ 1
Zoë Rillstone Run your code in Code Playground then add it in your question
15th Feb 2021, 10:28 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 1
Abhay Posting question with proper format is good thing that's why I said that
15th Feb 2021, 10:34 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 1
Zoë In condition if the purity is under 75% nothing should display print(" ")
15th Feb 2021, 11:07 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
+ 1
Simba I'm still getting the error on the last test case.
15th Feb 2021, 12:16 PM
Zoë
Zoë - avatar
+ 1
Mohasin Haque don't spam
15th Feb 2021, 7:34 PM
Sonic
Sonic - avatar
+ 1
Bro remove "and purity <= 100" You can add one more line in code to for invalid input(more than 100) Elif purity >100 : Print("invalid input please inter between range of 0 to 100")
16th Feb 2021, 8:56 AM
Divyanshu Pandey
Divyanshu Pandey - avatar
+ 1
"if purity>=99" that includes the upper range up to 100%...actualy ">=" it means that "greator than or equal to" ... Later on, try to have them in a single print function#
16th Feb 2021, 1:26 PM
Azad m. A.
Azad m. A. - avatar
+ 1
A Simple Guy (New Id) done now - sorry, I'm a complete noob!
16th Feb 2021, 1:47 PM
Zoë
Zoë - avatar
+ 1
Zoë how did you get the last test case to work?
21st Mar 2021, 11:02 PM
Zach Z
+ 1
First two's answers need to be "Accepted" This was my solution and it worked for all the tests: purity = float(input()) if purity >= 99.99: print("Accepted") elif purity >= 91.7: print("Accepted") elif purity >= 83.3: print("20K") elif purity >= 75.0: print("18K")
30th Apr 2021, 3:22 PM
Wouter Wessels
0
Zoë Rillstone Show your attempt
15th Feb 2021, 10:26 AM
R🍁🇮🇳
R🍁🇮🇳 - avatar
0
🇮🇳 R😻 why should they run code in playground and then add it to question ?
15th Feb 2021, 10:29 AM
Abhay
Abhay - avatar