Raising
Let's learn how to raise an error.
Errors can be raised using the raise
keyword:
1raise TypeError
The TypeError
in the example above was raised without a message.
To output an error message, you can pass a string inside the parentheses () after the error object:
1raise ValueError("Wrong value!")
Swipe to start coding
You have implemented the perimeter()
function that returns the triangle perimeter. This function receives the sides of a triangle (a
, b
, and c
). You need to add error raising to this function.
-
All received sides should be
int
orfloat
data type. In another case, the function should raise theTypeError
with the message"All sides should be int or float type"
. -
In a triangle, all sides should have a length greater than 0. In other cases, the function should raise the
ValueError
with the message"All sides should be greater than 0"
. -
In a triangle, the sum of two sides must always be greater than the third side (otherwise, the two sides would not reach each other through the third side). Therefore, if one of the received sides is greater than the sum of the other two, the program must raise the
ValueError
with the message"It's not a triangle"
.
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 4.35
Raising
Swipe um das Menü anzuzeigen
Let's learn how to raise an error.
Errors can be raised using the raise
keyword:
1raise TypeError
The TypeError
in the example above was raised without a message.
To output an error message, you can pass a string inside the parentheses () after the error object:
1raise ValueError("Wrong value!")
Swipe to start coding
You have implemented the perimeter()
function that returns the triangle perimeter. This function receives the sides of a triangle (a
, b
, and c
). You need to add error raising to this function.
-
All received sides should be
int
orfloat
data type. In another case, the function should raise theTypeError
with the message"All sides should be int or float type"
. -
In a triangle, all sides should have a length greater than 0. In other cases, the function should raise the
ValueError
with the message"All sides should be greater than 0"
. -
In a triangle, the sum of two sides must always be greater than the third side (otherwise, the two sides would not reach each other through the third side). Therefore, if one of the received sides is greater than the sum of the other two, the program must raise the
ValueError
with the message"It's not a triangle"
.
Lösung
Danke für Ihr Feedback!
Awesome!
Completion rate improved to 4.35single