I keep getting the error: TypeError: 'float' object is not callable on this section of code.
for data in list:
latitude = float(data[1])
longitude = float(data[2])
distance = ((6371*math.pi)/180)(math.sqrt((-35.276159-latitude)**2+(149.120893-longitude)**2))
print(distance)
When I print the type of latitude and longitude, they both return 'float'. Any help with what I could be doing wrong would be much appreciated!
You are iterating over your sequence and are calling the item data; this got you confused that data is the sequence.
You are also missing an operator between two expressions in parenthesis: distance = ((6371 * math.pi) / 180) MISSING OPERATOR HERE (math.sqrt((-35.276159 - latitude) ** 2 + (149.120893 - longitude) ** 2)) - this corresponds to (float)(float) after partial evaluation, which is attempting to call a float.
for item in seq:
latitude = float(item)
longitude = float(item)
distance = ((6371 * math.pi) / 180)**2 + (math.sqrt((-35.276159 - latitude) ** 2 + (149.120893 - longitude) ** 2))
print(distance)
Note:
Please do not use python keywords (list) to name variables.
Related
currently I've been trying to have my tuple variables: tempLentry,windLentry,dewpointLentry, be converted into an integer so I can use them in the math sequence. However I keep getting this error whenever it happens: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Entry'. I was hoping someone could point in the right direction, Thank you!
The code is below
def calc ():
#Tuple being recieved from askUser
tempLentry,windLentry,dewpointLentry = askUser()
#Converts string input from entries to integers for the calculations to work.
t = int(tempLentry)
ws = int(windLentry)
dp = int(dewpointLentry)
#Checks to see if inputed data for temperature and windspeed is less than 50mph and
greater than 3 mph
if (t < 50 and ws > 3):
windchill = (35.74 + 0.6215 * t - (35.75 * math.pow(ws,0.16)) + (0.4275 * t * math.pow(ws,0.16)))
else:
windchill = 0
You need to first convert the Entry to a string, and then cast it to an int.
You can get the Entry as a string by using .get()
t = int(tempLentry.get())
ws = int(windLentry.get())
dp = int(dewpointLentry.get())
I'm trying to calculate something in a function with numbers from an imported list.
def calculate_powers(velocities_list):
power_list = []
for i in velocities_list:
power = 8/27 * 1.225 * i * 8659
power_list.append(power)
print(power_list)
However, I get the following error:
File "C:\Users\Omistaja\PycharmProjects\7_1\wind_turbine.py", line 6, in calculate_powers
power = 8/27 * 1.225 * i * 8659
TypeError: can't multiply sequence by non-int of type 'float'
What to do?
try this :
def calculate_powers(velocities_list):
power_list = []
for i in velocities_list:
power = float(8/27) * 1.225 * float(i) * float(8659)
power_list.append(power)
print(power_list)
your variable 'i' is probably string, you need to cenvert it to number(float or int,..)
power = 8/27 * 1.225 * float(i) * 8659
can you provide example of your velocities_list?
I am looking to calculate the standard deviation for each column of a .csv file I have. Everything works up to this point but I keep getting the "TypeError: 'float object is not iterable" message. However, I need two of these values to be floats in order to retain the decimal places for accuracy. Is there a way to do this calculation with floats and not using iteration? Or is there an exception in the rule where I am able to use floats?
Here is the needed part of my code:
import math
fileChoice = input("Enter the file name: ")
inputFile = open(fileChoice)
headers = inputFile.readline().strip().split(',')
dataColumns = []
for i in headers:
dataColumns.append([])
rowCount = 0
for row in inputFile:
rowCount = rowCount + 1
comps = row.strip().split(',')
for j in range(len(comps)):
dataColumns[j].append(float(comps[j]))
l = 0
for entry in dataColumns:
mean = sum(dataColumns[l])/rowCount
stdDevSum = 0
for x in dataColumns:
stdDevSum = float(stdDevSum) + (((float(comps[row]) - float(mean))** 2) for row in range(rowCount))
stdDev = math.sqrt(stdDevSum / rowCount)
print(l + 1, headers[l], max(dataColumns[l]), min(dataColumns[l]), "{0:0.2f}".format(mean), stdDev)
l = l + 1
inputFile.close()
Edit:
Solution has been found
There's an error in this line:
stdDev = math.sqrt((sum((float(comps[l]) - float(mean)) ** 2) in range(rowCount)) / rowCount)
The error comes specifically from the expression sum((float(comps[l]) - float(mean)). When you do sum(something), Python tries to iterate over something. But in this case, the thing it's trying to iterate on is float(comps[l]) - float(mean), which is just a number. Hence the error: 'float' object is not iterable.
Also note that your use of in range(rowCount) is wrong. a in b means "return true if a is in b, return false otherwise". You were probably looking for the for i in iterable syntax.
Solution
I'm assuming that you want the sum of comps[row] - mean for each row. Try this:
stdDev = math.sqrt(sum( (float(comps[row]) - float(mean)) **2 for row in range(rowCount) ) / rowCount)
I am trying to make a model in python for an economics dissertation and in calculating a function:
elif wage_online == max(wages):
# after going to online you get the highest wage after cost of education
learning_benefit = int(np.int((1 + gamma_online_learning) * x[0]) * exp(np.int(bivariate_distribution[x[0] - 1, x[1] - 1])))
social_benefit = int(((1 + gamma_online_social) * np.int(x[1])) * exp(np.int(bivariate_distribution[x[0] - 1, x[1] - 1])))
sigma_social.append(social_benefit)
sigma_learning.append(learning_benefit)
I get the following error
/Users/sa/Documents/Dissertation/first_defiation.py:160: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
learning_benefit = int(np.int((1 + gamma_online_learning) * x[0]) * exp(np.int(bivariate_distribution[x[0] - 1, x[1] - 1])))
I tried to fix this by including the value in the exp function as np.int, but to no avail. does anyone know which variable the warning is coming from?
Looks like you missed the one place where int was needed, inside the square brackets:
social_benefit = (1 + gamma_online_social) * x[1] * exp(
bivariate_distribution[int(x[0] - 1), int(x[1] - 1)] # needed here for indexing
)
In future, you can use warnings.filterwarnings('error') to find the exact traceback of the warning, which would have pointed you to __getitem__
This question already has answers here:
TypeError: can't multiply sequence by non-int of type 'str'
(3 answers)
Closed 8 years ago.
I am new to python. I keep getting the error listed below. Can someone shed some light on the problem I am having? Thanks
volume = 4.0/3.0 * math.pi * (radius*radius*radius)
TypeError: can't multiply sequence by non-int of type 'str'
import math
radius = input("Enter Radius: ")
print("Radius: " + str(radius))
volume = 4.0/3.0 * math.pi * (radius*radius*radius)
print("Volume: " + str(round(volume,2)))
surface = 4.0 * math.pi * (radius*radius)
print("Surface Area: " + str(round(surface,2)))
You need to convert your string radius to a float or double.
Try this:
import math
radius = input("Enter Radius: ")
print("Radius: " + str(radius))
r = float(radius)
volume = 4.0/3.0 * math.pi * (r*r*r)
print("Volume: " + str(round(volume,2)))
surface = 4.0 * math.pi * (r*r)
print("Surface Area: " + str(round(surface,2)))
input returns a string. You can't multiply two strings together.
If you want to convert it to an int or float (or any other type), do it like this:
radius = float(radius)
You may have been confused by looking at example code from Python 2.x, where input called eval() on the string for you, so you got a float or int or list or call to os.system('rm -rf /') or whatever the user typed. This was confusing and dangerous, so they fixed it in 3.0, so now you get just the string, and it's up to you to decide what to do with it.