Factorial error- local variable referenced before assignment



Guys, Please help me with the error message.


well, so if we call the function with x = 2, then for the first run of the loop we have the following condition:

while 0 < 1 > 2:

well, 1 is not bigger then 2, so the condition evaluates to False, the loop never executes, factorial never gets defined. then returning factorial does indeed pose a bit of a problem

also, keep in mind that using factorial = x * a will set a new value for factorial every time, it doesn't add to the previous defined value of factorial


It looks like you've gone down a more complicated path than you really need to...
you can define this function in only 5 lines of code, and you only need a 'for loop'

Remember what the exercise states: "just multiply all the integers from 1 through x"... well, you can do that with a simple 'for loop' that takes values in the range of numbers up to and including x (remember to initialize a variable right below your function so that it can take the values from your loop and then return the 'result'


@stetim94 @sergiu3

Thanks guys!! Got it done now after few tries. :slight_smile: