0

The "Faculty " function, should return the faculty of the number it receives as a parameter. The faculty of a number n is 1 * 2 * 3 * 4 * ... * n. For example, faculty (5) should be 1 * 2 * 3 * 4 * 5 = 120. The function as it is written now always returns 0, no matter what number it receives as a parameter.

So, how do I fix the problem? (I'm just trying to learn, I'm new)

def faculty(integer): result = 1 for i in range(integer): result *= i return result 
2
  • try range(1,interger+1) Commented May 19, 2020 at 7:58
  • As you are computing faculty, I suggest also taking look at functools.reduce Commented May 19, 2020 at 8:06

3 Answers 3

2

The range statement takes a 0 as starting point. Therefore, the result is directly set to 0. You could change it to:

def faculty(integer): result = 1 for i in range(1, integer+1): result *= i return result 
Sign up to request clarification or add additional context in comments.

Comments

1

The method range(stop) generates the values [0;stop[ so you have the 0 which kill everything, and not stop. What you need is range(start, stop) with range(1, integer+1) to generates [1;value+1[

def faculty(integer): result = 1 for i in range(1, integer+1): result *= i return result 

When debugging, a nice way is in most time to use print to see what values are used to understand the behaviour of the code

Comments

1

Consider what values your loop is looping over.

for i in range(integer): print(i) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.