Factorial!


#1



Factorial


I would like the "ValueError" to appear when the function is called with a float number. But I get
an error message saying that float numbers are not allowed.


import math
def factorial(number):
    #n=str(number)
    
    if number>=0 and type(number)!=float(number):
        fact=math.factorial(number)
        return fact
     else:
        return ValueError
print factorial(1)
print factorial(0)
print factorial(9)
print factorial(2.5)

Replace this line with your code.


#2

you should check if type does not equal float:

type(number)!=float

what you did, will convert input to a float number, and then check if float type not equals float number


#6

Sorry, I don't understand what you mean.


#7

i literally give you the code?

there is a problem with your condition. lets say we have 2.5, you have the following condition:

type(2.5) != float(2.5)

which results in:

float != 2.5

this condition is true. Instead, you want to see if type of number equals float data type:

type(number)!=float

which would result in:

type(2.5) != float
float != float

which is false, else will run (as you want to achieve)


#8

Thanks. I changed it, and works as I intended.


#9