Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ISS Location Finder problem
#1
Hey guys,

I have been doing the 'Where is the Space Station?' project on the wonderful Raspberry Pi website and have encountered a problem. When I run the full code on Trinket online editor (as it says to) this error keeps popping up: 'KeyError: response on line 57 in main.py'. I have done some research on what a KeyError is but can't figure out the error. This is the link to the site: 'https://projects.raspberrypi.org/en/projects/where-is-the-space-station/' (leave out the apostrophes).

Here is my code:

import json
import turtle
import time
import urllib.request


url = 'http://api.open-notify.org/astros.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())

print('People in space: ', result['number'])

people = result['people']

for p in people:
  print(p['name'], 'in the', p['craft'])
  
url = 'http://api.open-notify.org/iss-now.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())

screen = turtle.Screen()
screen.setup(720, 360)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.bgpic('map.gif')

location = result['iss_position']
lon = float(location['latitude'])
lat = float(location['latitude'])

print('Latitude: ', lat)
print('Longitude: ', lon)

screen.register_shape('iss.gif')
iss = turtle.Turtle()
iss.shape('iss.gif')
iss.setheading(90)

iss.penup()
iss.goto(lon, lat)

lat = 52.183
lon = 0.216712239

location = turtle.Turtle()
location.penup()
location.color('yellow')
location.goto(lon,lat)
location.dot(5)
location.hideturtle()

url = 'http://api.open-notify.org/iss-pass.json'
url = url + '?lat=' + str(lat) + '?lon' + str(lon)
response = urllib.request.urlopen(url)
result = json.loads(response.read())

over = result['response'][1]['risetime']

style = ('Mono', 6, 'bold')
location.write(time.ctime(over), font=style)

print(over)
Thanks,
Birdwatcher.
Reply
#2
note that the url you construct is not correct
the correct one is http://api.open-notify.org/iss-pass.json....216712239
i.e. &lon=, not ?lon=

as a result the json response doesn't have the keys you expect
check http://api.open-notify.org/iss-pass.json....216712239
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hey Buran,

Thanks for the reply.

Birdwatcher.

P.S. The lat and lon I typed in are completely random btw. I think it is somewhere in England.

However, it still comes up with KeyError. Grrr!
Reply
#4
show your current code and full traceback
print the url to see what it is
print the result
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
import json
import urllib.request
 
lat = 52.183
lon = 0.216712239
 
url = 'http://api.open-notify.org/iss-pass.json'
url = url + '?lat=' + str(lat) + '&lon=' + str(lon)
response = urllib.request.urlopen(url)
result = json.loads(response.read())
 
over = result['response'][1]['risetime']
print(over)
Output:
1579558188
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Hi Buran,

What do you mean by full trace back and what do I need to do with the URL?

Huh Huh

Birdwatcher
Reply
#7
(Jan-20-2020, 07:34 PM)birdwatcher Wrote: What do you mean by full trace back
that is the whole message that you get when there is error, not just the last line

(Jan-20-2020, 07:34 PM)birdwatcher Wrote: what do I need to do with the URL
see line#8 in my example
also, it's better to use requests package. you need to install it from PyPi
same example with requests
import requests
 
params = {'lat':52.183, 'lon':0.216712239}
 
url = 'http://api.open-notify.org/iss-pass.json'
response = requests.get(url, params=params)
result = response.json()
 
over = result['response'][1]['risetime']
print(over)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Hi Buran,

Thank you for the correspondence. My code works perfectly now.

Birdwatcher.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help in designing a timestamp finder for chapter-less TV shows Daring_T 1 1,850 Oct-26-2020, 09:30 PM
Last Post: Daring_T
  problem about maze path finder Simurg 2 1,942 Aug-16-2020, 01:10 PM
Last Post: Simurg
  Location Named Entity Recognition Problem owais 7 6,146 Aug-08-2017, 09:43 AM
Last Post: lukecage

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020