25th Dec 2016, 11:37 AM
The Guayguy
The Guayguy - avatar
3 Answers
+ 4
You initial code: #Factorial Input Fee = float(input()) #Float operator Foo = float ( Fee ) #Factorial Operator def Factorial ( Foo ) : if Foo == 1 : return Foo else : return Foo * Factorial ( Foo - 1 ) print ( Factorial ) When you DEFine a function which take parameters, you write "def my_func(param1, param2)" for example, where "param1" and "param2" are variables internal to functions. You initialize/set them when you call the function... In your code, you define Factorial(Foo), at line 3 ( I count only real line of code, not comment and empty lines ), and you call it at last line for printing it. Here's your mistake: you attempt to initialize/set your Foo parameter at line 2, but in reality you declare a new variable different from this from your function parameter ^^ And when you call your function for printing the result, you pass the function itself as parameter to the print()... so finally, the program print the adress of the function ( and don't tell you there's an error, because there's not gor him ) in place of the result expected. Well, corrected code is ( add comment in end line for modified ones ): #Factorial Input Fee = float(input()) #Float operator #Foo = float ( Fee ) # no more necessary: you can delete this line #Factorial Operator def Factorial ( Foo ) : if Foo == 1 : return Foo else : return Foo * Factorial ( Foo - 1 ) print ( Factorial(Fee) ) # we call print, with the result of calling Factorial of Fee at parameter # [ and Fee is the parameter wich is passed to Factorial, and auto-copied in parameter variable "Foo" of Factorial ] # [ as if we write: Factorial(Foo=Fee) ]
25th Dec 2016, 12:04 PM
visph
visph - avatar
+ 2
The Foo Parameter in the function definition of factorial is a new local variable available within the function and not equal to the global Foo variable you defined earlier. Therefore you have to pass the actual Foo variable to the function when calling it. Just replace the "print(Factorial)" with "print(Factorial(Foo))". In addition to that you should modify your Factorial function, so that it returns 1 if you pass a 0 as argument.
25th Dec 2016, 11:58 AM
Simon
Simon - avatar
+ 1
"""first of all man you defined a function but you didn't call it the right way when you call a function like that factorial it will give you its presence in the memory in a hex code the right way to call a function is factorial(foo) """ Fee = float(input()) #Float operator Foo = float ( Fee ) #Factorial Operator def Factorial ( Foo ) : if Foo == 1 : return Foo else : return Foo * Factorial ( Foo - 1 ) print ( Factorial(Foo) )
25th Dec 2016, 11:59 AM
Ahmed Magdy
Ahmed Magdy - avatar