Math Equation with Python [closed] - python

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))

Related

I am Wrote this python code but every time it's give me wrong answer [closed]

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 have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)

Temperature conversion Table [closed]

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 need to write a program that displays Celsius to Fahrenheit temperature conversion in a table.
Question - What is the correct way to print a specific index within a list. My attempt comes from the answer to a similar exercise outlined here. Thanks in advance.
This is my code
temp = range(0 , 101, 10)
tbl = []
tbl2 = []
for i in temp:
cel = i
tbl.append(cel)
fah = (i * 9/5) + 32
tbl2.append(fah)
print(cel, fah)
print('Celsius\t\tFahrenheit')
print('-------------------')
print(tbl(0) + ':\t\t', tbl2(0))
print('-------------------')
This is my output
Functions are called with parenthesis(). Indexes are accessed with with brackets[0]:
print(tbl[0] + ':\t\t', tbl2[0])

Python ignores "if" statement, a possible scope problem? [closed]

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'm trying to write a weight converter which splits the given input and act accordingly however if statement is completely ignored. So far my search revealed that it might be a scope problem but I'm pretty green so couldn't apply any solutions to my specific code.
def weight_convert(answer):
seperated = answer.split(" ")
unit = seperated[1]
weight = int(seperated[0])
if unit.lower == "lbs":
converted = weight * 0.45
else:
converted = weight % 0.45
return converted, unit
Try this:
def weight_convert(answer):
seperated = answer.split(" ")
unit = seperated[1]
weight = int(seperated[0])
if unit.lower() == "lbs":
converted = weight * 0.45
else:
converted = weight % 0.45
return converted, unit

My code outputs an error, I don't understand why [closed]

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)

What's wrong in this python code? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
def vector(sample_list):
for item in sample_list:
sums = 0
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude
print(vector([2,3,-4]))
Why this code doesn't give the correct magnitude?
It gives the last value of the vector in function call.
change sums=0 position
def vector(sample_list):
sums = 0
for item in sample_list:
square = item**2
sums = square + sums
magnitude = sums**0.5
return magnitude

Categories