Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: unsupported operand type(s) for -: 'unicode' and 'unicode', coords

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.

like image 652
user1869558 Avatar asked Jan 13 '13 21:01

user1869558


People also ask

How do I fix unsupported operand type s?

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 .

What does unsupported operand type S for -: int and list mean?

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.

What does unsupported operand type S for /: STR and STR mean?

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


2 Answers

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'])
like image 152
asermax Avatar answered Sep 22 '22 00:09

asermax


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.")
like image 28
Paulo Scardine Avatar answered Sep 21 '22 00:09

Paulo Scardine