Hello,
I am working on perfect_numbers exercise
the last two tests which test the raise exception don’t pass : I have the message “ValueError not raised”. I don’t understand why.
below my code:
try:
if number >= 1:
somme = 0
divisors = []
if number>1 :
limit = int(math.sqrt(number))
i = 2
while i <= limit :
if number%i == 0:
divisors.append(i)
if i!=number/i:
divisors.append(number/i)
i = i+1
for n in divisors :
somme+=n
somme = somme + 1
if somme == number :
return "perfect"
elif somme > number :
return "abundant"
else:
return "deficient"
else:
raise ValueError("Classification is only possible for positive integers.")
except ValueError as err:
print(err)
thank you for help
You’ll have to copy/paste your code inside a code block (triple backticks) so we can see it, because the link you’ve shared only works for you.
```
// the code goes here
```
Also, did you read all the instructions carefully?
This particular exercise requires that you use the raise statement to “throw” a ValueError
if the classify()
function is passed a number that is not a positive integer. The tests will only pass if you both raise
the exception
and include a message with it.
To raise a ValueError
with a message, write the message as an argument to the exception
type:
# if a number to be classified is less than 1.
raise ValueError("Classification is only possible for positive integers.")
Okay, so having seen the code, here’s a hint:
The function is expected to raise an error if the input is less than 1. What does your implementation do in this case? Take a look at the try/except
block specifically.
Hi @NunoPIRES 
To tag onto Cool-Katt’s hint: We never, ever (in any exercise we have on the track so far) have you print anything.
Nor do we have more than a few exercises that require that you use a try.....except
— and this exercise is not one of them.
Not that you can’t use a try.....except
— but you need to handle the error in a way that the test can “see” it. Right now, test is failing because the error message that’s raised
never gets to where the test can validate it.
thank you for your answers, I have finally fixed the problem by adding “raise” in last line of the except block
I will keep in mind for the other exercise that no exercise require try...except
Thank you again for your help
1 Like