+ 2

[Solved] Can anyone explain me why is this code giving error?

Learning property decorator for the first time I don't quite understand why it is giving error on line 4 and line 26 Here's my code: https://code.sololearn.com/clAlr934O4lq/?ref=app.. I understand now.. there was a problem using the property decorator before.. and maybe I somehow didn't write the full code.. Here's the solution I found it online https://code.sololearn.com/c5DMKz8LPsN3/?ref=app thanks everyone for helping me.. 😊

25th Jul 2022, 1:51 PM
Moon
Moon - avatar
6 Réponses
+ 7
Moon , maybe the use of try... exept can help: @temperature.setter def temperature(self, value): print("Setting value...") try: if value < -273.15: raise ValueError("Temperature below -273 is not possible") self._temperature = value except ValueError as v_e: print(v_e)
25th Jul 2022, 4:47 PM
Lothar
Lothar - avatar
+ 4
Moon , i have used your code and have applied the modifications with try... except. it should run, just try it. https://code.sololearn.com/cDKnZ9bQ91JS/?ref=app
26th Jul 2022, 10:23 AM
Lothar
Lothar - avatar
+ 3
People can help you quicker when you put your code in a script on sololearn playground: Go to Code section, click +, select the programming language, insert your code, save. Come back to this thread, click +, Insert Code, sort for My Code Bits, select your code. This way, it is much better readable and other can test your code.
25th Jul 2022, 1:58 PM
Lisa
Lisa - avatar
+ 2
It is giving the error because you programmed it to raise an error when one tries to input an invalid temperature value. :)
25th Jul 2022, 3:04 PM
Lisa
Lisa - avatar
+ 1
last line for what? if you delete it your code will work fine.
25th Jul 2022, 2:31 PM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 1
Lothar, I tried it.. it shows syntax error.. and if I remove the raise error part it shows no output 😓
26th Jul 2022, 8:29 AM
Moon
Moon - avatar