Thread: Running into error due to usage of break function.

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2015
Posts
18
Rep Power
0

Running into error due to usage of break function.

I am coding a function that generates the factorial of a number. As a reminder to all the factorial
of 3 is 3*2*1=6. Etc. I wrote a piece of code that works fine except for a particular if statement
that I had to add in especially for the case of 1, the factorial of which is 1. My code works fine
without the if statement that is created for the case of 1. The code runs fine if I enter any positive
number other than 1 as the argument for the function "factorial". I am expecting it to print the
string 1 to the console, but it doesn't. I will share my code now:

Code:
```def factorial(x):
count = x
list_of_nums = []
while count > 0:
list_of_nums.append(count)
count -= 1
print list_of_nums
for i in xrange(len(list_of_nums)):
if len(list_of_nums) == 1:
print "1"
break
elif i == 0:
fact_of_nums = list_of_nums[i] * (list_of_nums[i] - 1)
elif i < (len(list_of_nums) - 1):
fact_of_nums = fact_of_nums * (list_of_nums[i] - 1)
print fact_of_nums

factorial(1)```
ERROR:

Code:
```Traceback (most recent call last):
File "/Users/Tom/Documents/Python/additionaltestingground.py", line 18, in <module>
factorial(1)
File "/Users/Tom/Documents/Python/additionaltestingground.py", line 16, in factorial
print fact_of_nums
UnboundLocalError: local variable 'fact_of_nums' referenced before assignment
[Finished in 0.0s with exit code 1]```
If someone would help me figure out what's going wrong here I'd appreciate it highly. Thank you
2. No Profile Picture
Contributing User
Devshed Novice (500 - 999 posts)

Join Date
May 2009
Posts
667
Rep Power
40
You don't need the break statement as the length of the list is one, so the for loop will only execute once and exit. Note that you will get an error on "print fact_of_nums" as that is never declared when a one is passed to the function. What do you want it to be in this case?

Code:
```	for i in xrange(len(list_of_nums)):
if len(list_of_nums) == 1:```

• Will-O-The-Wisp agrees
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2015
Posts
18
Rep Power
0
"Note that you will get an error on "print fact_of_nums" as that is never declared when a one is passed to the function." Isn't that a good reason to use a
break? Also the code errs when the break is taken out so the error has more to do with the if statement in actuality.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
May 2015
Posts
18
Rep Power
0
Actually, I'm sorry. I can just set fact_of_nums equal to 1 and exclude the break. This solves the problem. Thank you.
5. If python programming is a long term plan for you, please read Guido's essays:
https://www.python.org/doc/essays/
In particular I'm thinking of the optimization anecdote.

Here are 4 other definitions of factorial, which I share because your version is the most complicated one I've seen.
Code:
```import math, functools, operator

math.factorial

factorial_a = lambda n:functools.reduce(operator.mul, range(2,n+1), 1)

def factorial_b(n):
result = 1
for i in range(2, n+1):
result *= i
return result

def factorial_c(n):
if n < 2:
return 1
return n * factorial_c(n-1)```
(yes, in j factorial is the monadic ! verb)