Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorials Problem
#1
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
Reply
#2
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.
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Hint?
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Computing factorials Truman 6 4,077 Mar-14-2018, 06:38 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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