making factorial code with python what's the problem,? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
def factorial(n):
if (n>1):
return n* factorial(n-1)
def factorial(n):
if (n>1):
return n* factorial(n-1)
else:
return 1
The first code has an error but the second function runs properly
what is the difference between the two programs?
what is the problem with the first code?
thank you

The problem is that when the first code evaluates for n<=1, there is no returned value. This causes the function to (by default) return None. Now while evaluating for 2, the program says
return n* factorial(n-1)
That is equivalent to;
return 2*None #Since factorial(1)=None
Hence It gives an error as NoneType cannot be multiplied by an integer

Related

Return a value or assigning a value to a variable and then return - best practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have two functions
def sum(a, b):
result = a + b
return result
and
def sum(a, b):
return a + b
From the point of view of good programming practices and software engineering, which solution is better? Return a value or assigning a value to a variable and then return? Why?
2nd option is good because it saves space of a variable but at times, we use more variables on purpose for the sake of clarity. We always have to maintain a good balance between clarity and space.

What is the difference between void function and frutial function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Now I am watching PY4E youtube, the difference between void and fruitial function does not touch me well. just return value?
Also, if the void function has a function PRINT(SOMETHING)
even though it has not return value, but it has some value or result?
isn't it? I am little bit confused.
In Python, all functions return something, but some return None, which is ignored. These are called "void". Functions that return anything else are called "fruitful".
For example, list.append():
>>> a = []
>>> a.append(1)
Note how nothing was printed after a.append(). But it did actually return None, which we can confirm by printing its return value:
>>> print(a.append(2))
None
print as well returns none:
>>> print(print(a.append(3)))
None
None
And user-defined functions which don't have a return statement return None.

Function doesn't Break [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to break out or restart a function in python 2. Putting a return statement should stop all execution. Same goes for restarting the function.
def function():
...
if len(lst) == 1:
return value
print 'foo'
else:
function()
print 'foo'
In this case 'foo' would be printed twice.
The return statement only stops the execution of the current instance of the function.
Since you're doing a recursion, you will still get the other calls of that function running until they hit their own return (if any).

Two vectors inside a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new on Python.
I want to create a function with two vectors inside. I tried like this
def twovectors((velocity1,length1),(velocity2,length2)):
but I have a message error like
SyntaxError: invalid syntax.
Please, need help.
You cannot put tuple in the function definition as a parameter. Check Multiple Function Arguments or 8.6. Function definitions in the Python language reference.
Try something like this this:
def twovectors(vector1, vector2):
velocity1, length1 = vector1
velocity2, length2 = vector2
# Other code...
I used tuple unpacking to expand provided tuple arguments.
You write functions in python in this way :
def twovectors(velocity1, velocity2):
# You can get the length of those vectors after you get inside the function
len1, len2 = len(velocity1), len(velocity2)
// Your code here
return whateveryouwantto

When calling multiple methods, will they add? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
In python, if a person has the following code, will the 2 methods called automatically add? how does this work?
example:
def trip_cost(city,days):
return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days)
Will hotel_cost(days) and rental_car_cost(days) both be added together when they are returned to trip_cost(city,days) ??
If the return values of the methods you invoke have a defined + operator they will add up.
If the 3 return numeric types (for example: float, int): yes
But if 2 return numeric and one returns a str: no
The return values may also be instances of objects defined by yourself which may have a defined + operator (which may or may not be compatible with numeric types or with strings)
Each function call [e.g. plane_ride_cost(city)] is evaluated and the results will be added together (in left to right order as +'s have equal precedence) then the overall result returned.

Categories

Resources