5/15 problem


#1

Help! I don’t understand what’s wrong with it. When I put it into online compiler it says "Kill signal (SIGKILL)
"

def factorial(x):
  fin = x
  if x == 0:
    return 1
  else:
    while x != 0:
      if x < 0:
        fin = fin * (x + 1)
      else:
        fin = fin * (x - 1)
  return fin

It just thinks a lot of time and in the end doesn’t give any explanation of problem


#2

Hey @quentas1

There are a couple of things you need to fix in your code.

First, I think perhaps you’ve misunderstood how to calculate a factorial.

We can calculate the factorial (n!) of any non-negative integer n by multiplying it by every positive integer that is less than n. So

5! = 5 x 4 x 3 x 2 x 1 = 120

You can’t calculate the factorial of a negative integer.

Second, the response you’re getting from the Codecademy editor of Kill signal (SIGKILL) suggests that you have an infinite loop.

A while loop will continue to run so long as the condition you’ve given it remains true. So, the following loop will never end:

while True:
  print "I am a never ending while loop!"

In your code, the while loop will continue to run so long as x != 0. Are you sure that’s the correct condition for your while loop?


#3

You are right, I misunderstood the task and hint. Also I just realized how weird my code is, haha) thanks


#4

No worries, sometimes the instructions can be a little confusing - especially if you’ve been staring at the same ones for a while!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.