This question already has answers here:
Understanding factorial recursion [duplicate]
(3 answers)
Closed 5 years ago.
I'm having trouble figuring out how does this exactly work. Can anyone please explain?
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
It gets the factorial of the value x. The value of x is whatever you enter since x = int(raw_input) gets the user input. For example if x=3, then fact(3) will be 3×2=6....
Related
This question already has an answer here:
Python function returns None, unclear why
(1 answer)
Closed 2 years ago.
I created a function to square some numbers. I was wondering why my code didn't print.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
A = [2,0,-3]
print(Square(A))
Add a return value.
def Square(A):
for i in range(len(A)):
A[i] = A[i]**2
return A
print(Square(A))
This question already has answers here:
Understanding factorial recursion [duplicate]
(3 answers)
Python recursive Factorial Function [duplicate]
(6 answers)
Understanding recursion in Python
(4 answers)
Closed 2 years ago.
I apologize if this is a dumb or repeating question firstly. I have been learning python for about 2 weeks now and I keep having small problems with loops. For example.
def factorial(n):
if n < 2: # I understand once the number becomes < than 2 the loop continues
return 1. #and here we return the value 1
result = n * factorial(n-1) #why do we subtract 1 from n?
return result
factorial(5) #my random number
I am having trouble understanding why you subtract 1 from n. Shouldn't you add 1 instead to include the number?
In my mind it looks like this:
result = n(1) * factorial((1)-1) # doesn't n stay = to 1 or am i mistaken?
Thank you for you help. Sorry again if its an idiot question.
E.g. first you call the function with 5, then would you again call it with 5 and multiply, it will overflow the recursion limit and endless loop.
So, you need to decrement by 1 and then call by 4, 3 and so on and when it is less than 2 return 1.
This question already has an answer here:
Python script run through IDLE has no output
(1 answer)
Closed 2 years ago.
In python code, I've written:
x = 1
y = 2
def add_nums(first, second):
sum = first + second
return sum
And when I do:
add_nums(x, y)
It returns nothing what is wrong with the code?
The function returns its local sum variable. Perhaps you meant to print it
print(add_nums(x, y))
This question already has an answer here:
Why Python recursive function returns None [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to create a recursive function in Python (as part of an online course) which takes two numbers and returns the highest common denominator between them, however, the function won't return the value.
My code is:
def gcdRecur(a, b):
x = min(a,b)
y = max(a, b)
if x == 0:
return y
else:
gcdRecur(x, y%x)
When I test the function with
gcdRecur(2,12)
or
gcdRecur(6,12)
nothing is returned. I know the function works because if I print y before I return it then the correct denominator is printed to the console, I'm just not sure why the function doesn't output y.
It's probably something obvious but as I'm new to Python I'm not sure what I'm doing wrong.
Any help would be much appreciated.
You need to return something in both branches:
else:
return gcdRecur(x, y%x)
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 5 years ago.
# Example for Algorithm Case Study
def naïve(a, b):
x = a
y = b
z = 0
while x > 0:
z = z + y
x = x - 1
return z
print naïve(4,5)
The output should be 20. Because of syntax error in print statement, I am not getting the answer.
print in Python 3 is a function, meaning you need to call it with parenthesis:
print(naïve(4,5))