0

import  os

import urllib2

import json

while True :

    ip= raw_input ("what's the ip :")

    url="http://ip-api.com/json/"

    response = urllib2.urlopen(url + ip)

    data = response.read()

    values = json.loads(data)

    print ("IP:" + values['query'])
    print ("city:" + values['city'])
    print ("ISP:" + values['isp'])
    print ("COUNTRY:" + values['country'])
    print ("region:" + values['region'])
    print ("time zone:" + values['timezone'])

what should i add instead of these 2 ?

print ("latitude:" + values['lat'])
print ("longitude:" + values['lon'])
break 
I_love_vegetables
  • 1,575
  • 5
  • 12
  • 26
robert
  • 1
  • 1
    I'm having a hard time understanding your question. What exactly is the problem with your code? – purple Jul 24 '21 at 20:12
  • Next time please remember to post the actual problem, not just the lines causing the problem. – Thomas Jul 27 '21 at 09:15
  • Related [Print Combining Strings and Numbers](https://stackoverflow.com/questions/12018992/print-combining-strings-and-numbers) change '+' to ','. `print ('latitude:', values['lat'])`? – mcalex Jul 27 '21 at 09:23

1 Answers1

0
$ curl http://ip-api.com/json/1.1.1.1
{"status":"success","country":"Australia","countryCode":"AU","region":"QLD","regionName":"Queensland","city":"South Brisbane","zip":"4101","lat":-27.4766,"lon":153.0166,"timezone":"Australia/Brisbane","isp":"Cloudflare, Inc","org":"APNIC and Cloudflare DNS Resolver project","as":"AS13335 Cloudflare, Inc.","query":"1.1.1.1"}

From this, we can see that lat and lon are numbers in the JSON, not strings:

"lat":-27.4766,"lon":153.0166

So Python is complaining, because you can't add a number to a string.

To make it work, either convert the number to a string first:

print ("latitude:" + str(values['lat']))
print ("longitude:" + str(values['lon']))

Or, for a more modern and readable solution, use f-strings:

print (f"latitude:{values['lat']}")
print (f"longitude:{values['lon']}")
Thomas
  • 174,939
  • 50
  • 355
  • 478