Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorial
#1
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?
Reply
#2
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.
Reply
#3
(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
Reply
#4
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.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Functional style:


from functools import reduce
from operator import mul


factorial = lambda n: reduce(mul, range(1, n + 1))
print(factorial(5))
Output:
120
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
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.
Reply
#7
(Jul-18-2019, 05:52 AM)ndc85430 Wrote:
(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

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?
Reply
#8
(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 factorial
The 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 factorial
Since factorial is not defined for negative values, we return None. Now it can return an integer or a None type.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
(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?

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 factorial
Thanks. This clarified a lot.
The 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 factorial
Since factorial is not defined for negative values, we return None. Now it can return an integer or a None type.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,237 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 67,848 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial, repeating Aldiyar 4 2,791 Sep-01-2020, 05:22 PM
Last Post: DPaul
  factorial using recursive function spalisetty06 12 4,051 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 1,877 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,294 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Want to print each iteration of factorial program sbabu 10 4,566 Jan-09-2020, 07:25 AM
Last Post: perfringo
  Factorial sketch(python 3) KingArthur526 1 1,969 Sep-25-2019, 01:51 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020