0

So i'm trying to work with floats as elements in Python lists but I keep getting this error. I tried making each value a string and then converting it to a float when calling the array to print but that doesn't seem to work either

P1 = [45.100000, ‐65.400000]
print(P1[0])
SyntaxError: invalid character in identifier

Attempt #2

P1 = ["45.100000", "‐65.400000"]
print(float(P1[1]))
ValueError: could not convert string to float: '‐65.400000'

I have a feeling the issues have to do with the negative value in front of the 2nd elements (@ index 1)

1
  • I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol? Commented Nov 13, 2018 at 0:23

3 Answers 3

2

There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1, and check the unicode, it gives:

>>> ord('‐')
8208

Whereas the proper negative or subtraction sign should be:

>>> ord('-')
45

Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus

Sign up to request clarification or add additional context in comments.

Comments

1

I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;

P1 = [45.100000, -65.400000]

1 Comment

Yes, that seems to fix the problem -_-
1

This is because your - is not a minus sign but a hyphen character:

>>> "‐65.400000".encode('utf-8') # copy from your example
b'\xe2\x80\x9065.400000'

>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'

\xe2\x80\x90 is a hyphen character, see here: your hyphen is U+2010 and the hyphen-minus is U+002D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.