-1

I'm unable to get list values to have int() called upon them. The list is build from user input(), a split() and append(). Code looks like:

coords = []

fcoords = input("Enter first coordinate pair:\n")

urlat, urlon = fcoords.split()
coords.append(urlat)
coords.append(urlon)

scoords = input("Enter second coordinate pair:\n")
lllat, lllon = scoords.split()
coords.append(lllat)
coords.append(lllon)

for coord in coords:
    print (int(coord))

Gives me a ValueError: invalid literal for int() with base 10: '-122.4444'.

I can switch the int(coord) with float(coord) but that's just extra work since I am converting decimal degree to dms. Using int() gets the base without remainder which is what I want!

Is the split() method not correct here, or are extra characters being hidden?

  • The error is correct (if little suprising) as far as i see. Try `int(float(coord))`. – Radosław Cybulski Jul 05 '17 at 19:40
  • The extra work you're mentioning is almost certainly irrelevant. – zneak Jul 05 '17 at 19:41
  • @zneak I shouldn't have to call `float()`, `int(-122.4444)` works and yields `-122` as `-122` is a real number integer! –  Jul 05 '17 at 19:42
  • You're saying that `int(-122.4444)` should be the same as `int("-122.4444")`. Would you say the same thing about `-122.4444 + 55` and `"-122.4444" + "55"`? – zneak Jul 05 '17 at 19:44
  • @madeslurpy dude, why do you keep arguing against everybody trying to help you? – Josep Valls Jul 05 '17 at 19:46
  • Possible duplicate of [Parse String to Float or Int](https://stackoverflow.com/questions/379906/parse-string-to-float-or-int) – Alter Jul 05 '17 at 19:49

4 Answers4

1

Since the literal is -122.4444, it represents a float; convert it to float first and then round to int: print(int(float(coord)))

Błotosmętek
  • 12,717
  • 19
  • 29
  • Gah. I knew about that but I was trying to avoid it. I shouldn't have to do this. `int(-122.4444)` works and yields `-122`. Why must I call `float()`? –  Jul 05 '17 at 19:41
  • 1
    Because `int` is overloaded; given a string argument, it tries to read a literal representing an integer value from it; given a float, it does rounding to an integer. Two distinct uses. – Błotosmętek Jul 05 '17 at 19:43
  • @madeslurpy You can also try `eval('int(%s)' %'-122.4444')`. – cs95 Jul 05 '17 at 19:43
  • BTW, isn't it strange that the same `in` is used both as operator (`if x in something:`) and a keyword (`for x in something:`)? – Błotosmętek Jul 05 '17 at 19:49
1

'-122.4444' is not an int, so you can't parse it as int.

If you don't want to use float(coord) you could do:

print (int(coord.split('.')[0]))

This will ignore the decimal part of your number and take only the integer part

Roberto
  • 2,115
  • 24
  • 32
  • That's not correct. `int(-122.4444)` will yield `-122`. Why does it not work if it comes from a list. That is the real question! –  Jul 05 '17 at 19:43
  • @madeslurpy It's because the `int` function treats strings and floats differently. – cs95 Jul 05 '17 at 19:44
  • `int(-122.4444)` will work, but `int('-122.4444')` will not work, remember that the result of input is a string – Roberto Jul 05 '17 at 19:44
0

You have a string which matches a float and you are trying to convert that to a int, which is not supported by int. As others suggested use int(float(your_str)).

So the problem is not the split, however, you could also use regexp which might be better suited for user input, as it is much more flexible.

To match an integer you could use:

pattern = r'(^[-+]?[0-9]+)\.'
re.findall(pattern, your_input_string)

EDIT:

Regexp might not work perfectly, you have to test

meow
  • 2,062
  • 2
  • 17
  • 27
-1

You can't change float string to int.

change your split() to:

urlat, urlon = fcoords.split('.')

and:

lllat, lllon = scoords.split('.')

Your code will work.

gaback
  • 638
  • 1
  • 6
  • 13
  • OP didn't provide us with an example input, but if the coordinates are a pair of Lat/Longs, it's likely your code would split something like `122.144 89.322` into `122` and `144 89` and `322`. But this is a close answer, I think. Splittng on the space is necessary first, I think. – dckuehn Jul 05 '17 at 20:17