Click here to Skip to main content
15,887,304 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
def fact_iterative(n):
fact=1
for i in range(n):
fact=fact *(i+1)


return fact
n=int(input("enter:"))
print("ITERATIVE FACTORIAL:",fact_iterative(n))

What I have tried:

def fact_iterative(n):
fact=1
for i in range(n):
fact=fact *(i+1)


return fact
n=int(input("enter:"))
print("ITERATIVE FACTORIAL:",fact_iterative(n))
Posted
Updated 30-Aug-22 0:54am

If you want to learn Python then you need to do it properly. Go to The Python Tutorial — Python 3.7.8 documentation[^] and work through it.
 
Share this answer
 
v2
It's Python - you have to be careful about the white-space indentations (below, each indent is a tab, but you can use spaces - but don't mix them!):

Python
def fact_iterative(n):
	fact=1
	for i in range(n):
		fact=fact *(i+1)
	return fact

Python
n=int(input("enter:"))

Python
print("ITERATIVE FACTORIAL:",fact_iterative(n))


once you get that right it works correctly.
Also if you're copy-pasting or typing this into a python interpreter, each chunk needs to be entered separately.
 
Share this answer
 
Comments
Member 14897676 24-Jul-20 8:58am    
can you explain me how this works:-

def fact_iterative(n):
fact=1
for i in range(n):
fact=fact *(i+1)
return fact
Quote:
can you explain me how this code works?

You are kidding, right?
The code is trivial: it declares a function (called fact_iterative) that takes a parameter value and returns it's factorial.
It then gets a value from the user, calls the function (passing the value) and prints the returned value.

The body of the function is a trivial loop!

TBH, if you can't work that out for yourself, then you need to go back to the start of your course, and read all the way though - there is nothing complicated going on here at all!
 
Share this answer
 
Comments
Member 14897676 24-Jul-20 9:14am    
can you explain how this works:-

def fact_iterative(n):
fact=1
for i in range(n):
fact=fact *(i+1)
return fact
OriginalGriff 24-Jul-20 10:07am    
What part of it do you not understand?
I'm not being funny or awkward - that's trivial code so I have no idea what part is giving you problems.
Quote:
Can someone explain me this code?

Rather than a magic explanation, watch the code perform.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900