full code is HERE
HTML code
<input type="hidden" id="Latitude" name="Latitude" value={{Longitude}} />
<input type="hidden" id="Longitude" name="Longitude" value={{Longitude}} />
document.getElementById("Latitude").value = position.coords.latitude;
document.getElementById("Longitude").value = position.coords.longitude;
app.py
Latitude = request.form['Latitude']
Longitude = request.form['Longitude']
messages = database.returnMessagesinRange(float(Latitude),float(Longitude))
database.py
def returnMessagesinRange(longitude,latitude):
allMessages = Messages.find()
messagesinRange = []
for current in allMessages:
if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
if messagesinRange == None:
messagesinRange = [current['text']]
else:
messagesinRange.append(current['text'])
return messagesinRange
When this is run, i get
if ((current['longitude']-longitude) * (current['longitude']-longitude) + (current['latitude']-latitude)*(current['latitude']-latitude)) <= 1:
TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode'
Anyone know why this is happening? thanks.
The Python "TypeError: unsupported operand type(s) for /: 'str' and 'int'" occurs when we try to use the division / operator with a string and a number. To solve the error, convert the string to an int or a float , e.g. int(my_str) / my_num .
The Python "TypeError: unsupported operand type(s) for /: 'list' and 'int'" occurs when we try to use the division / operator with a list and a number. To solve the error, figure out how the variable got assigned a list and correct the assignment, or access a specific value in the list.
The Python "TypeError: unsupported operand type(s) for -: 'str' and 'str'" occurs when we try to use the subtraction - operator with two strings. To solve the error, convert the strings to int or float values, e.g. int(my_str_1) - int(my_str_2) .
Both the longitude and latitude retrieved from the request and the database are strings (unicode strings) and you are trying to operate on them as if they were numbers.
You should first get the int
or float
representation of such strings to be able to operate on them as numbers (using -
, *
, etc)
You can do that by creating a int
or float
object passing the string as a parameter
latitude = int(request.form['Latitude'])
or
latitude = float(request.form['Latitude'])
Unlike in PHP, Python will not auto-convert from string to float. Use:
errors = []
try:
latitude = float(request.form['Latitude'])
except ValueError:
# do something about invalid input
latitude = 0.0
errors.append(u"Invalid input for Latitude.")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With