Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to calculate percentage in Python, by hand this works fine but in python this formula for some reason does not work. I tried multiple ways but just not getting correct results.
getPercent = lambda part, whole: float(1 - (float(part)/float(part))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Output:
0.0
output should be:
0.95
Just use whole instead of part in the denominator.
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
Try it online!
you haven't used whole in your lambda function
getPercent = lambda part, whole: float(1 - (float(part)/float(whole))) * 100
Val1= float(3194.15)
Val2= float(3163.84)
getPercent(Val2, Val1)
# output 0.9489222484855064
You need to divide by whole. To get just two decimal places:
get_percent = lambda part, whole: float(1 - part / whole) * 100
val_1 = float(3194.15)
val_2 = float(3163.84)
a_float = get_percent(val_2, val_1)
print("{:.2f}".format(a_float))
Returning:
0.95
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am running a big loop and I want to see the progress. I want the code to print the index of the iteration when the remainder of that number with 1e4 is zero. What I ve tried so far looks like this:
print(i if (i%1e4==0))
But it doesn seem to work. I cant use any functions such as qdtm because I am also using numba and it does not accept it. Any help?
The conditional operator requires an else value. If you don't want to print anything when the condition is false, you need to use an ordinary if statement, not a conditional expression.
if i % 1e4 == 0:
print(i)
If it really has to be a single statement, you could make use the the end argument to print() to print nothing, and then include the newline in the output you print.
print(str(i) + '\n' if i % 1e4 == 0 else '', end='')
for i in range(int(1e5)):
if (i%1e4==0):
print(i)
Outputs:
0
10000
20000
30000
40000
50000
60000
70000
80000
90000
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
def solve(meal_cost, tip_percent, tax_percent):
tip_percent=float(meal_cost * (tip_percent / 100))
tax_percent=float(meal_cost * (tax_percent / 100))
total_cost=meal_cost + tip_percent + tax_percent
return (total_cost)
print(solve(12, 20, 8))
The answer should be 15, but the above is giving 12.0. Not sure why.
when I tried printing tip_percent, it's giving 0.0.
tip_percent calculated as follows:
float(meal_cost * (tip_percent / 100))
I'm learning Python at the beginning stage.
Your results are consistent with using Python 2. In Python 2 / is integer division if the numbers to divide are integers. To force float math change 100 to 100.0:
def solve(meal_cost, tip_percent, tax_percent):
tip_percent=float(meal_cost * (tip_percent / 100.0))
tax_percent=float(meal_cost * (tax_percent / 100.0))
total_cost=meal_cost+tip_percent+tax_percent
return total_cost
print(solve(12,20,8))
Or change to using Python 3.
Results:
15.36
That's because you are testing with Python 2, which returns 12.0.
In Python 3 the same code would return 15.36.
The reason is Python changed how they handle division in Python 3.
For example:
3 / 2 = 1 (in Python 2.7.6)
3 / 2 = 1.5 (in Python 3.4.1)
To fix that divide by 100.0 (not by 100).
Exampels you could find here: https://riptutorial.com/python/example/2797/integer-division
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm trying to calculate something, but the code outputs an error
I have even tried splitting the operation in many single parts, to not do all at one time, I have tried by setting and int() output, but it doesn't work too
import math
x_coo = 20
y_coo = 30
x = 50
y = 80
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
force = (81*24*25)/ (distance(e.x_coo,e.y_coo, a.x,a.y)^2)
print(force)
Error:
TypeError: unsupported operand type(s) for ^: 'NoneType' and 'int'
You forget to return the result of the operation inside the function:
import math
x_coo = 20
y_coo = 30
x = 50
y = 80
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
return dist
force = (81*24*25)/ (distance(e.x_coo,e.y_coo, a.x,a.y)^2)
print(force)
You need to return a value from the function distance. Python loves returning values, and if you do not declare what your function returns, Python assumes you want to return None.
Here's what you should have:
def distance(x_coo,y_coo,x,y):
dist = math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
return dist
You missed return on distance(), i.e.:
def distance(x_coo,y_coo,x,y):
return math.sqrt((e.x_coo-a.x)**2 + (e.y_coo-a.y)**2)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I trying to pass the input to the function but my code has a problem.
I have position with the (x, y) coordinates for each points.
I would be appreciate if someone help me to fix my program.
def distance(ball1,ball2):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(float(d))
ball1=[2,10]
ball2=[3,5]
distance(ball1,ball2)
Error has been fixed, But the output is not accurate and the result always is 1.
May you please help to fix that too? thanks
The issue is that you are accessing the third element in ball2, with ball2[2]. However, there are only two elements in the list. You need to change it to ball2[1]-ball1[1]:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)
You cannot access the third element.
Code:
def distance(ball1=[],ball2=[]):
d=(((ball2[0]-ball1[0])**2)+((ball2[1]-ball1[1])**2))**(1/2)
print(d)
ball1=[1,1]
ball2=[3,3]
distance(ball1,ball2)
def distance(ball1=[], ball2=[]):
d = (((ball2[0] - ball1[0]) ** 2) + ((ball2[1] - ball2[1]) ** 2)) ** (1 / 2)
print(d)
ball1 = [1, 1]
ball2 = [3, 3]
distance(ball1, ball2)
The 1 result is due to division.
In Python 2 1/2 does integer division with 0 result.
In Python 3 1/2 works computes to 0.5
Just replace 1/2 with 0.5 in your code.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to obtain solution for math equation with python such as follows:
# Open netCDF file
fd1 = nc.Dataset(nc_file1, 'r')
# Read variables from the netCDF file
rng1 = fd1.variables['range'][:]
tim1 = fd1.variables['time'][:]
pwr1 = fd1.variables['pwr'][:]
dpl1 = fd1.variables['dpl'][:]
nfft1 = fd1.variables['nfft'][0]
pn1 = fd1.variables['pnoise'][:]
# Close netCDF file
fd1.close()
# Specify beam
ibeam1 = 0
# Time convertion from seconds to hours
tim1 = tim1/3600.0
# Select data and transpose
p_plot1 = pwr1[ibeam1]
for it1 in range(len(tim1)):
p_plot1[it1] = p_plot1[it1] - pn1[ibeam1][it1] - 10.*np.log10(nfft1)
p_plot1 = p_plot1.transpose()
**#Determine Height**
A=6378.1370
Tetha=math.cos((37.5 * math.pi) / 180)
for j in range(len(tim1)):
for i in range(len(rng1)):
D[i]=sqrt(A**2+(rng1[i]**2)+(2*A*(rng1[i])*Tetha)
height1(i,j)=(D[i]-A)
From the math equation (Determine Height) I got error. It might be of syntax for equation in python. Actually, I focused on three variable of data that is tim1, rng1, and pwr1. From rng1 data I will obtain height1, just by convert rng1 to height1 by using that equation.
Thank you in advance.
The problem is the row
D[i]=sqrt(A**2+(rng1[i]**2)+(2*A*(rng1[i])*Tetha)
The parentheses don't match up, just add one at the end like this and it will work.
D[i]=sqrt(A**2+(rng1[i]**2)+(2*A*(rng1[i])*Tetha))