2 views
in Python
closed

I'm attempting to compose a basic factorial function in Python, yet upon execution, this one only sits idle and intrinsically crashes the console for some unusual explanation.

Kindly remember that I just started coding with Python about seven days prior, and am accustomed to utilizing other lower-level OOP languages, so kindly pardon any undeniable slip-ups.

Look at my current function:

# initialise factorial function:

def factorial(n):

i = 1 # initialise incrementing variable

while i < n:

n = n * i

i = i + 1

return n # return result

Here, The function prints nothing.  Kindly help me.

closed

by (19k points)

To fix your factorial function, you need to update the loop condition and the way variables are updated. Here's a concise version of the corrected code:

def factorial(n):

result = 1

for i in range(1, n + 1):

result *= i

return result

In this modified code, the loop iterates over the range from 1 to n+1, multiplying result by each value of i. Finally, the updated result is returned as the factorial value.
by (26.4k points)

Try the below function, which works with n>=0

def factorial(n):

r = 1

i = 2

while i <= n:

# Use shorter version

r *= i

i += 1

return r

or else, it works with n>0:

def factorial(n):

i = n

while i > 1:

i -= 1

n *= i

return n

Are you interested to learn the concepts of Python? Join the python training course fast!

Watch this video tutorial on how to become a professional in python

by (25.7k points)
The issue with your current factorial function is that it enters an infinite loop when i reaches a value that is greater than or equal to n. This occurs because the loop condition i < n is never updated within the loop itself.

To fix this issue and make your factorial function work correctly, you need to modify the loop condition and the way n is updated. Here's an updated version of your code:

def factorial(n):

result = 1  # Initialize the result variable to store the factorial

i = 1  # Initialize the incrementing variable

while i <= n:

result *= i

i += 1

return result

In this updated code, I have changed the loop condition to i <= n to ensure that the loop runs until i reaches the value of n. Additionally, I have modified the way n is updated by assigning the multiplication of result and i back to result itself using the *= operator.

Now, when you call factorial(n), it will correctly calculate the factorial and return the result. Make sure to pass a valid value of n to the function to obtain the desired factorial output.
by (15.4k points)
The issue with your current factorial function is that it gets stuck in an infinite loop due to the loop condition not being updated properly. To resolve this, you need to modify the loop condition and the way the variables are updated. Here's an version of your code:

def factorial(n):

result = 1  # Initialize the variable to store the factorial

i = 1  # Initialize the incrementing variable

while i <= n:

result *= i

i += 1

return result

In this revised code, I have made adjustments to ensure the loop runs until i reaches the value of n. Additionally, I have modified the updating of variables by using the *= operator to multiply result by i and updating i using +=. With these changes, the function should correctly calculate the factorial and return the result when you call factorial(n). Remember to provide a valid value for n to obtain the expected factorial output.