Factorial - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Factorial (/thread-19880.html) |
Factorial - leodavinci1990 - Jul-18-2019 I created the following function in Python 3.7. def getFactorial(n): factorial = 1 for x in range(1, n+1) factorial = factorial * x print(factorial)I am using the Python Shell to output the following commands: >>>getFatorial(5) returns 120 >>>print(getFatorial(5)) returns None why does the second command return None? RE: Factorial - stranac - Jul-18-2019 Because the value of getFactorial(5) is the one you return from the function.Since you have no return statement in your function, python returns None .
RE: Factorial - ndc85430 - Jul-18-2019 (Jul-18-2019, 05:04 AM)leodavinci1990 Wrote: >>>getFatorial(5) returns 120 Using the word "returns" here is wrong - the function doesn't return that value (it returns None as mentioned above). What you're seeing is the output from the call to print in the function
RE: Factorial - perfringo - Jul-18-2019 Some nitpicking about code style: - It's good to follow naming conventions set for variables and functions in PEP 8: Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability. So instead of getFactorial one should have get_factorial - One should aim to write DRY (Don't Repeat Yourself) code. Therefore factorial = factorial * x could be written factorial *= x
RE: Factorial - DeaD_EyE - Jul-18-2019 Functional style: from functools import reduce from operator import mul factorial = lambda n: reduce(mul, range(1, n + 1)) print(factorial(5))
RE: Factorial - RUBI - Jul-18-2019 I'm afraid >>>print(getFatorial(5)) will return none coz the function you created is on a local scope so factorial is a local variable which can't be used in a global scope unless a global statement is used. RE: Factorial - leodavinci1990 - Jul-19-2019 (Jul-18-2019, 05:52 AM)ndc85430 Wrote:(Jul-18-2019, 05:04 AM)leodavinci1990 Wrote: >>>getFatorial(5) returns 120 Thanks for clearing this up, so I get from this if say that function namely, getFactorial had a return statement in it with a certain data type returned say integer for example, will it return int? RE: Factorial - ichabod801 - Jul-19-2019 (Jul-19-2019, 01:02 AM)leodavinci1990 Wrote: if say that function namely, getFactorial had a return statement in it with a certain data type returned say integer for example, will it return int? Not exactly. You return an expression, often just a single name. But there is no set data type for what is returned. You generally design your function to always return the same type, but there are cases where you might plan on returning different types depending on the input. Python gives you the flexibility to do that. def getFactorial(n): factorial = 1 for x in range(1, n+1) factorial = factorial * x return factorialThe modified getFactorial above always returns an integer, but we might do this: def getFactorial(n): if n < 0: return None factorial = 1 for x in range(1, n+1) factorial = factorial * x return factorialSince factorial is not defined for negative values, we return None. Now it can return an integer or a None type. RE: Factorial - leodavinci1990 - Jul-19-2019 (Jul-19-2019, 01:14 AM)ichabod801 Wrote:(Jul-19-2019, 01:02 AM)leodavinci1990 Wrote: if say that function namely, getFactorial had a return statement in it with a certain data type returned say integer for example, will it return int? |