简体   繁体   中英

Calculating a factorial using loops in Python3

I am currently studying Software Development as a beginner and I have a task in my programming class to calculate and display a factorial using a loop. I've been given the pseudo-code and have to translate it into true code and test it in the REPL to make sure it returns the expected results.

I almost have it but I've run into two issues that I just can't seem to resolve.

1) The function is returning an extra line of "None" after the calculation and

2) The answer is displaying over multiple lines when I want it to display on a single line.

My current code (which does return the correct answer) is as follows:

def calcFactorial(number):
    factorial = 1
    print(str(number)+"! =", number)
    for count in range (1, number):
        if number-count > 0:
            factorial = factorial*number-count
            print("x", str(number-count))
    factorial = factorial*number
    print("=", factorial)

When I test, using 3 for example, the REPL returns the following:

>>> print(calcFactorial(3))
3! = 3
x 2
x 1
= 12
None

So I have the correct answer but with an extra line of "None" which I would like to remove (I believe it has something to do with the print function?) and I don't know how to format it correctly. Any help would be much appreciated.

your function calcFactorial(3) prints already, so you shouldn't call it with

print(calcFactorial(3))

just call it with

calcFactorial(3)

without the print function.

you might think about calling the function calc_and_print_factorial() in order to make it clear, that this function does already the printing

Regarding your second question:

Blockquote 2) The answer is displaying over multiple lines when I want it to display on a single line.

You can fix it by using a single print statement:

def calcFactorial(number):
    factorial = 1
    string = str(number) + "! = " + str(number)
    for count in range (1, number):
        if number-count > 0:
            factorial = factorial*(number-count)
            string = string + " x " + str(number-count)
    factorial = factorial * number
    print(string + " = " + str(factorial))

This will give you:

IN: calcFactorial(3)
OUT: 3! = 3 x 2 x 1 = 6

On a side note: you might want to think of how to implement this recursively. Maybe that comes later in your class but this would be one of the first go-to examples for it.

Adding to the blhsing's answer , you should choose between these built-in ways to print the "returned" value.

First way:

def calcFactorial(number):
    ... # <- Your function content
    return factorial

Then, call your function with a print() to get the explicitly returned value, as you can see in the return factorial line. See this reference for more details :

print(calcFactorial(3))

Second way:

Having the same function definition with its return statement, just call the function with its instance statement:

calcFactorial(8)

By default, python will print the returned value without a print()

Third way:

Just call the function (without the explicit return statement, this will return a "None" (null-like) value by default), using the print() method. Do NOT use print() inside another print() .

Your calcFactorial function does not explicitly return a value, so it would return None by default, so print(calcFactorial(3)) would always print None .

You should make the calcFactorial function return factorial as a result at the end:

def calcFactorial(number):
    factorial = 1
    print(str(number)+"! =", number)
    for count in range (1, number):
        if number-count > 0:
            factorial = factorial*number-count
            print("x", str(number-count))
    factorial = factorial*number
    print("=", factorial)
    return factorial

So I have the correct answer but with an extra line of "None" which I would like to remove

You are printing the return value from your function. In this case, you haven't specified a return value with a return statement so the function automatically returns None .

To fix the problem, you should return a value from your function. Note that you don't need to call print() for final answer because the REPL already does this for you.

Note that the REPL will automatically print the return value for you, so you can just type calcFactorial(3) instead of print(calcFactorial(3)) .

Additionally, you are not getting the correct answer. You should get 6 instead of 12 . It looks like you are trying to count down from number and multiplying each number together. You can get the same result by counting up from 1. This will lead to much simpler code and avoid the error.

If you want to understand why your code isn't doing the correct thing, look closely at factorial = factorial*number-count and think about the order of operations that are used to calculate the result here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM