5. Factorial


#1

I’m wondering if how I’m thinking about this problem works. I keep getting indentation errors, and ended up seeing the solutions and ofc it’s short and sweet lol. The solution has every indent 2 tabs (4 spaces). I’ve been using 1 tab and it’s been working fine. I don’t remember ever having to switch to 2 tabs.

The problem asks to create a function that returns the factorial of an integer x.

def factorial(x):
  n = 0
  for i in len(range(0, x)):
    while n <= x:
      y = 1 * (n + 1)  	  
      return y
      n += 1
    else:
      return x

#2

What is the purpose of this loop? i is defined but never used.


#3

My thought is that I want it to loop x amount of times. So if x is 5 or 6 its looping that amount of times. Because I’m increasing the count instead of decreasing, I didn’t know how to stop the count really so I did this.

Oh I see what you’re saying. I don’t need that line at all right?


#4

Right, the while loop should be all you need. There are still a couple of concerns to rectify, though.


#5

A post was split to a new topic: Indent problems


#6