Who can explain how this code is not an error? [duplicate] - python

This question already has answers here:
Recursive function in simple english [duplicate]
(6 answers)
Closed 11 months ago.
I was making some exercise to train myself and the exercise asked me to do a program that calculates fractals, very simple, i've done in about 1-2 minutes and it work, but looking at his solution it return x multiplicated by the function itself? how does this run? I know maybe it's a stupid question but i think it might be useful.
def fract(x):
if x == 0:
return 1
return x * fract(x - 1)
print(fract(int(input())))

Here's a walk through of whats going on.
First, you call fract(int(input())). The input method gets a response from the user and parses that to an int. and then calls fract on that int.
Say we enter 3. So our print statement evaluates to fract(3).
fract(3) returns 3 * fract(2)
fract(2) is called and that returns 2 * fract(1)
fract(1) is called and that returns 1
So putting it altogether and substituting function calls for what they return we get fract(3) returns 3 * (2 * (1)).

Related

Why function does nothing or returns nothing [duplicate]

This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 10 days ago.
I am just testing a function however the function returns nothing. I can't quite understand the rules. New to programming.
What I've tried:
Def func0(x, y):
if x < y:
z = (x*y) - x
return z
else:
w = (y*y) - 1
return w
This returns nothing when called.
For example I'm expecting if I call the function:
func0(5,6)
For it to return 25
Is there a better way to achieve or write this?
Thank you
I'm expecting if I call the function: func0(5,6) For it to return 25
If you just call the function like that, then it does return the value, but you aren't doing anything with the returned value, so it appears like nothing was returned.
You need to call it like this which saves the returned value in a variable:
myresult = func0(5,6)
And then you could do something with myresult, like print it.

What does "if 2 < a < b: do someting" mean in Python? [duplicate]

This question already has answers here:
Simplify Chained Comparison
(2 answers)
Closed 2 years ago.
I am learning Python and trying to understand the following if statement. May I know what does it mean?
if 2 < a < b:
do something.
Thank you!
An if statement is a check of a condition and if this condition is true it will execute the code after the : .
In this case the Code prior to your line should have two variables a and b which both contain numbers.
If a is greater value than 2 and b is an even greater value than a, for example a is 3 and b is 4, or a is 10 and b is 200, the following Code will be executed. „Do something“ is just a place holder for the code that should be executed.
The best for you would be to search and read about if and if else statements for python to find some real life examples.

How can I honor parentheses in evaluating arithmetic expressions? [duplicate]

This question already has answers here:
Math operations from string [duplicate]
(8 answers)
Evaluating a mathematical expression in a string
(14 answers)
Closed 3 years ago.
I am fairly new to python/ programming in general and i am trying to write a function that will convert an equation passed in as a string to its numeric representation and do some basic calculations. I am having some trouble with the parenthesis as i am not sure how to represent them for order of operations.
Any help of tips would be greatly appreciated. Thank you!
EquationAsString ="( 2 + 3 ) * 5"
def toEquation(EquationAsString):
Equation = EquationAsString.split(' ')
#store info in list and use it like a stack, check the type etc.
answer = 25
return answer
You can use the eval method to do such a thing.
Example:
print(eval('(2+3)*5'))
Output:
25
And if you really wanted to put it in a function:
def evaluation_string(input):
print(eval(input))
Example
def evaluation_string(input):
print(eval(input))
string_equation = '(2+3)*5'
evaluation_string(string_equation)
Output:
25

Python - Using while loops in functions [duplicate]

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 5 years ago.
I'm trying to create a function that uses a while loop to count up from one to a number given by a user. The code executes as I intend it to but returns None at the end. How do I get rid of the None? Here's the code.
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
print (printFunction(int(input())))
You can use this code to prevent none, tough its just the last line changed
def printFunction(n):
i = 1
while i <= n:
print(i)
i+=1
printFunction(int(input()))
In the last line you were using
print(printFunction(int(input()))) which was getting you None after printing the results.
Instead just use printFunction(int(input())). This will not print None. You can also use a message to ask user like printFunction(int(input("Enter a number"))). Since there is noting getting returned you no need to use print.

Recursion with one argument [duplicate]

This question already has answers here:
calculate length of list recursively [duplicate]
(2 answers)
Closed 7 years ago.
So, my goal is to count the number of arguments in a list using recursion. However, as I'm only to use one argument in the function call, I don't know how to solve it without a second "count" argument.
So far I have this, where I accumulate the 1's together.
def countElements(a):
if a==[]:
return []
else:
return [1] + countElements(a[1:])
def main():
a=[3,2,5,3]
print(countElements(a))
main()
Instead of returning an empty list in the base case, return 0, and instead of [1] + countElements(a[1:]), return 1 + countElements(a[1:]). That way, you're just keeping the running count instead of getting a list back.

Categories