I want to write script to calculate the volume. The first function calculates area of the base. The second takes this value and multiply it with the height. Then I'd like to write the value of the volume. Where is the mistake?
def base_area(a, b):
a = 2
b = 3
s = a * b
return s
def volume(s):
h = 7
V = h * s
print (V)
It doesn't make sense to pass a, b as parameters to base_area() function, because inside, you are assigning constant values to a and b. That method should look like:
def base_area(a, b):
s = a * b
return s
so you use the values passed. This function can be written in a more concise way:
def base_area(a, b):
return a * b
Then the volume() method should receive 3 parameters, a, b and h(height):
def volume(a, b, h):
return base_area(a, b) * h
Here, you are calling base_area() passing a and b. From this call, you get the area, so you multiply it by h and return it.
Test:
print volume(2, 3, 7)
>>> 42
Related
My aim is to take a triple (A, B, C), compute three "neighbours", and then output the maximum of each of those neighbours to a list.
For example, the neighbours of (sqrt(6), 4*sqrt(3), 9*sqrt(2)) are
(sqrt(3)*sqrt(2), 3*sqrt(2), 4*sqrt(3))
(4*sqrt(3), 35*sqrt(3)*sqrt(2), 9*sqrt(2))
(sqrt(3)*sqrt(2), 9*sqrt(2), 14*sqrt(3))
so the values 14*sqrt(3), 36*sqrt(6), 4*sqrt(3) would be the output.
When I try this:
A = 1*sqrt(6)
B = 4*sqrt(3)
C = 9*sqrt(2)
def nbhs_1(triple):
X = triple[0]
Y = triple[1]
Z = triple[2]
print((X.canonicalize_radical(), (X * Y - Z).canonicalize_radical(), Y.canonicalize_radical()))
def nbhs_2(triple):
X = triple[0]
Y = triple[1]
Z = triple[2]
print((Y.canonicalize_radical(), (Y * Z - X).canonicalize_radical(), Z.canonicalize_radical()))
def nbhs_3(triple):
X = triple[0]
Y = triple[1]
Z = triple[2]
print((X.canonicalize_radical(), Z.canonicalize_radical(), (X * Z - Y).canonicalize_radical()))
result_1 = nbhs_1((A, B, C))
result_2 = nbhs_2((A, B, C))
result_3 = nbhs_3((A, B, C))
print(result_1)
print(result_2)
print(result_3)
l = [max(result_1), max(result_2), max(result_3)]
I get 'NoneType' object is not iterable.
The main problem is that you are not structuring the function properly:
It is recommended that you expose your arguments within the function call. Don't def nbhs_1(triple), do instead def nbhs_1(X, Y, Z). In this way you can actually have one single function that does what you want (easier to maintain)
Return your result. At the moment you are printing the outcome of the function call but you are not returning those results.
I'm also not sure the canonicalize_radical() call is also done properly. Python is object-oriented and by writing var.canonicalize_radical() you are inferring that var should itself know about this function (e.g. the function is part of var) but that sounds wrong. The correct call may be canonicalize_radical(var)
Basically, this should be closer to a correct solution:
A=1*sqrt(6)
B=4*sqrt(3)
C=9*sqrt(2)
def nbhs(X, Y, Z):
out1 = canonicalize_radical(X)
out2 = canonicalize_radical(X*Y-Z)
out3 = canonicalize_radical(Y)
return out1, out2, out3
l = [max(nbhs(A, B, C)), max(nbhs(B, A, C)), max(nbhs(C, B, A))]
The problem is that you are not calling the functions nbhs_1, nbhs_2, and nbhs_3 and also the functions aren't returning any values
from math import sqrt
A=1*sqrt(6)
B=4*sqrt(3)
C=9*sqrt(2)
triple = (A, B, C)
def nbhs_1(triple):
X=triple[0]
Y=triple[1]
Z=triple[2]
return (X.canonicalize_radical(),(X*Y-Z).canonicalize_radical(),Y.canonicalize_radical())
def nbhs_2(triple):
X=triple[0]
Y=triple[1]
Z=triple[2]
return (Y.canonicalize_radical(),(Y*Z-X).canonicalize_radical(),Z.canonicalize_radical())
def nbhs_3(triple):
X=triple[0]
Y=triple[1]
Z=triple[2]
return (X.canonicalize_radical(),Z.canonicalize_radical(),(X*Z-Y).canonicalize_radical())
l=[max(nbhs_1(triple)),max(nbhs_2(triple)),max(nbhs_3(triple))]
I am trying to solve an equation for variable 'X' in python where some of the variables in the equation ('ABC, PQR') are output from a function 'calculations'. The problem is, in order to get an output from the function, I need to pass variable X as an argument itself. I am kind of stuck in a loop here. I tried two different approaches but didn't get any success. Is there a way I can solve the equation?
Any help/direction is really appreciated.
My first approach is to start with a small value and run a loop. I tried to use math.isclose() but receive 'math bound error' once the values go off the range and it runs into an infinite loop.
The second approach is to write the complete expression and use scipy.optimize fsolve() but I am unable to understand how to properly implement it.
# function
def calculations(X, a, b, c):
ABC = X*a*b + c
XYZ = X*b*c + a
PQR = X*a*c + b
return ABC, XYZ, PQR
# ABC, PQR is the output from a function which uses X as input
# solve for X
func = 2*ABC + sqrt(PQR*ABC) + X*100*0.5
# Approach 1
X = 0.001
step = 0.001
while True:
# function call
Var_ABC, Var_XYZ, Var_PQR = calculations(X, a, b, c)
func = 2*Var_ABC + math.sqrt(Var_PQR * Var_ABC) + X*100*0.5
if (math.isclose(func, 0.0, rel_tol=0.1) == True):
break
else:
X = X + step
# Approach 2
# Since I don't know the value of X, how can I get the output from the function and then solve it again?
func_output[0] = calculations(X, a, b, c) # ABC
func_output[2] = calculations(X, a, b, c) # PQR
func = 2* func_output[0] + math.sqrt (func_output[0] * func_output[2] ) + X*100*0.5
from scipy.optimize import fsolve
desired_output_X = fsolve(func, [0.01, 1])
This may help you getting started with fsolve:
# function of X
def func(X):
func_output = calculations(X, a, b, c)
func = 2* func_output[0] + math.sqrt (func_output[0] * func_output[2]) + X*100*0.5
return func
# extra arguments for calculations function, dummy values used: set them as desired
a,b,c = 1,2,6
# initiating X = 0.01 and solve for X
desired_output_X = fsolve(func, x0 = 0.01)
I have a list of single parameter functions in python to which I would like to apply the same scaling/translation operation.
The scale/translation for a function f(x) would be as follows:
f'(x, a, b, c, d) = a + b* f( c*(x - d) )
So for example if my original function is defined as:
f(x) = 3*x
I would like to modify it to be:
f'(x, a, b, c, d) = a + b * (3*(c*(x - d)))
Ideally using a decorator (or some other reusable operation that I can apply to each function in the list).
Is there a pythonic way to achieve this such that every transformation doesn't need to be hardcoded for each function I have?
Apologies if the question is unclear - thanks in advance for any suggestions.
def f(x):
return 3*x
def makeFPrime(func):
def inner(x, a, b, c, d):
return a + b * (3*(c*func(x-d)))
return inner
This gives for example:
f2 = makeFPrime(f)
f2(2,1,1,1,2)
1
f2(2,1,1,1,0)
19
Alternatively, use makeFPrime as a decorator:
def makeFPrime(func):
def inner(x, a, b, c, d):
return a + b * (3*(c*func(x-d)))
return inner
#makeFPrime
def f(x):
return 3*x
Then f(2,1,1,1,2) returns 1
This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 months ago.
Suppose I have a function like:
def eklid(p, a, b,):
x = [1, 0]
y = [0, 1]
r = [a, b]
q = [0]
n = 0
while r[n+1] != 0:
q.append(r[n] // r[n+1])
r.append(r[n] % r[n+1])
x.append(x[n] - x[n+1] * q[n+1])
y.append(y[n] - y[n+1] * q[n+1])
if p == 0:
print(r[n], "=", r[n+1], "*", q[n+1], "+", r[n+2])
elif p == 1: # extended print
print(r[n+2], "\t", x[n+2], "\t", y[n+2], "\t", r[n+2], "=", a, "*", x[n+2], "+", b, "*", y[n+2])
elif p == -1:
k =1
else:
print("wrong input")
n += 1
return x, y, r, q, n,
I want to use x and r from it in this function:
def cong_solv(x, r, b,):
result = x/r
int_result = int(result)
return int_result
How can I do that?
# Here, a=x, b=y, c=r, d=q, e=n
a, b, c, d, e = eklid(h, i, k)
# Assuming based on your function definitions you want the
# same value as the third argument
final_result = cong_solv(a, c, k)
You get the return values from eklid and save them into variables. You then use those variables to call the next function.
Of course, in a real code you should name your varialbes better than I did in this example. I deliberately did not call the variables the same names as inside the function to demonstrate that you don't have to.
One way would be to call the eklid() function from inside the cong_solv() function. Something like this should work:
def cong_solv(x, r, b):
p = "foo"
b = "bar"
x, y, r, q, n = eklid(p, a, b)
result = x/r
int_result = int(result)
return int_result
In python, when you return more than one variable, it returns a tuple.
You can retrieve the value by its index (returned_value[0], returned_value[1]) or unpack the tuple like Mike Driscoll said (a, b, c, d = eklid(h, i, k)).
Since I got two downvotes, I am going to give you better (I hope) explanation:
Everytime you return more than one value, it returns a tuple.
def my_function():
a = 10
b = 20
return a, b
print type(my_function()) # <type 'tuple'>
But if you return just one value:
def my_function():
a = 10
return a
print type(my_function()) # <type 'int'>
So if you want to use your value, you can:
Unpack tuple values like this
a, b = my_function()
This way you get your return values in the same order you return inside my_function.
Rewriting your code, you can simply do:
a, b, c = eklid(10, 20, 30) # it will return a tuple
And call your other function:
cong_solv(a, b, 20)
In my honest opinion I would return a dict. With dict you can be explicit because your values have key names.
Inside your eklid return function:
return d # d = {"what_x_means": x,
# "what_y_means": y,
# "what_r_means": r,
# "what_q_means": q,
# "what_n_means": n}
And retrieve for its key:
d["what_x_means"]
d["what_r_means"]
Similar to How do you return multiple values in Python?
Return as an tuple (x,y,r....) or an array and assign to tuple / array respectively.
Or assign them to class variables and access them
Say I have a function
def equals_to(x,y):
a + b = c
def some_function(something):
for i in something:
...
Is there a way to use c that was calculated by equals_to as a parameter for some_function like this
equals_to(1,2)
some_function(c)
You need to return the value of c from the function.
def equals_to(x,y):
c = x + y # c = x + y not a + b = c
return c # return the value of c
def some_function(something):
for i in something:
...
return
sum = equals_to(1,2) # set sum to the return value from the function
some_function(sum) # pass sum to some_function
Also the function signature of equals_to takes the arguments x,y but in the function you use a,b and your assignment was the wrong way round, c takes the value of x + y not a + b equals c.
Strongly recommend: http://docs.python.org/2/tutorial/