a factorial of a positive integer n, written as n!, is defined as:
n*(n-1)(n-2)…*1 if n is 0 n! is defined as 1 if n is negative, n! is undefined
An example is: 12! = 12*11*10*9*8*7*6*5*4*3*2*1
Write a program that 1. Inputs a positive integer from the user. If the integer is not positive, it displays an error message 2. Prompts the user to either have the factorial calculated by sequential programming (Option 1) or by recursion (Option 2)
Option 1: using one of the top down iterative methods (e.g. while, for) find the factorial of any positive integer (including 0).
Option 2: using recursion (see text section 6.3), find the factorial of any positive integer (including 0)
Submit to this assignment a Word document (version 2010 or earlier) that contains the source code of your program and screen shots running it with both options and, for each option, with 0, 9, and -4
Hints: you will need to define a function to perform this task using recursion don’t try this with too large a number --- that may generate an error because of the memory it takes to perform
I seem to have figured out how to do the functions; however, I cannot seem to get them to work in the main() function.
When I run my program, the Menu() function executes; however, after I enter 1 or 2, my program returns
(Traceback (most recent call last): File "C:/Users/user/Documents/Prjct4", line 59, in <module> main() File "C:/Users/user/Documents/Prjct4", line 54, in main num = factorial() UnboundLocalError: local variable 'factorial' referenced before assignment)
The following is what I have so far:
def Menu():
print
print ("For Sequential Programming Calculator, Enter 1")
print ("For Recursion Calculator, Enter 2")
print
while True:
choice = input("Enter your choice here: ")
if (choice >= 1) and (choice <=2) and (int(choice)==choice):
return choice
else:
print ("Invalid Choice! Please enter 1 or 2")
def factorial():
num = 1
while n >= 1:
num = num * n
n = n - 1
return num
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print "The factorial of",num,"is",factorial(num)
def recur_factorial():
if n == 1:
return n
else:
factorial= n*recur_factorial(n-1)
return factorial
num = int(input("Enter a number: "))
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print "The factorial of 0 is 1"
else:
print "The factorial of",num,"is",recur_factorial(num)
def main():
print
print("Factorial Calculator")
print
while True:
choice = Menu()
if choice == 1:
num = factorial()
elif choice == 2:
factorial = recur_factorial()
main()
If anyone could help me figure this out, I would greatly appreciate it! Thank you!
You have many bugs in your program.
However, the one that is first caused you a problem is that in your main
code, you are assigning a value to a variable called factorial. But factorial is supposed to be a function - as per your earlier definition. Why are you assigning the result to a variable anyhow? You don't do anything with it. Maybe you mean print factorial(num)
.
The next problem you encountered is that you have uninitialised variables all over the place. I assume, from the traceback in your comment, that you changed the code in main to pass num
into factorial
- like factorial(num)
. But where do you expect num
to get its value from?
You have some code (twice) that asks the user for a value for num
, but it is in a place where it will never be executed - after the return
s in your functions.
Perhaps you mean for this code to be in main
before you call the factorial functions?