Python Forum
Factorials Problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Factorials Problem (/thread-5108.html)



Factorials Problem - OmarSinno - Sep-19-2017

3. Factorials.
Write a program factorials.py that prints the factorials of the odd numbers between 1 and 10. Your
program should contain a function factorial(n) that returns the factorial of an integer n.
Hint. Write and test the factorial(n) function before you use it in the loop.

I don't even know what Factorials are so help please?

Thanks to the internet I got this for now but I don't understand a line off it:
import sys
n = int(sys.argv[1])

def factorial(n):
	if n == 0:
		return 1
	else:
		return n* factorial(n-1)
		

for i in range(1,11):
	factorial(n)
Thank you in advance! Pray


RE: Factorials Problem - stranac - Sep-19-2017

https://en.wikipedia.org/wiki/Factorial

If you try understanding the problem, maybe you can come up with a solution you understand.
Anything you write on your own will be better for learning purposes than copying code you don't understand at all.


RE: Factorials Problem - ichabod801 - Sep-19-2017

Also, the example you found uses recursion, which is more advanced topic in programming. If you are just beginning, I would suggest using a loop to solve the problem instead.


RE: Factorials Problem - OmarSinno - Sep-20-2017

Hint?


RE: Factorials Problem - OmarSinno - Sep-20-2017

def factorial(n):
    if n==0:
        return 1
    else:
        recurse = factorial(n-1)
        result = n*recurse
        return result

for n in range(11):
    if n%2 !=0:
        print(factorial(n))
Homework is done! :D