in the below code the

- Code: Select all
`print "inside the function", calculate_here`

shows me that the code does arrive at the correct answer

but then the final answer given (by 'return') is wrong

the problem seems to be the way "return calculate_here" works

Please help me to fix this problem

- Code: Select all
`print "Factorial attempt #2"`

def factorial(given_number):

calculate_here = given_number

while given_number > 0:

print "inside the function", calculate_here

calculate_here = calculate_here * (given_number - 1) # = 4 * 3;

given_number = given_number - 1 # = 3

return calculate_here

print "Factorial: 4! = 24. My answer = ",

print factorial(4)

#>>> 24 NOT 14

print "Factorial: 5! = 120. My answer = ", factorial(5)

#>>> 120 NOT 24

print "Factorial: 6! = 720. My answer = ", factorial(6)

#>>> 720 NOT 35

print ""

print "Factorial attempt #3"

def factorial(given_number):

index = 1

# calculate_here = given_number

while index <= given_number:

print "inside the function", calculate_here

calculate_here = index * (index + 1) # = 4 * 3;

index = index + 1 # = 3

return calculate_here

print "Factorial: 4! = 24. My answer = ",

print factorial(4)

#>>> 24 NOT 14

print "Factorial: 5! = 120. My answer = ", factorial(5)

#>>> 120 NOT 24

print "Factorial: 6! = 720. My answer = ", factorial(6)

#>>> 720 NOT 35