python turtle goto 不允许负浮点数
python turtle goto not allowing a negative float
我正在尝试使用 "turtle" 在世界地图上显示国际 Space 站 (ISS) 的位置。我从 API 中获取了经度和纬度。然后将坐标保存到变量"lon"和"lat".
但是当我使用 iss.goto(lon, lat)
时,我收到一个 TypeError。我相信这是由于经度和纬度坐标有时是负数造成的,所以浮点数以“-”为前缀。
谁能帮我解决这个问题?
import tkinter
import turtle
import json
import urllib.request
url = 'http://api.open-notify.org/iss-now.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
location = result['iss_position']
lat = (location['latitude'])
lon = (location['longitude'])
print('latitude: ', lat)
print('longitude: ', lon)
screen = turtle.Screen()
screen.setup(3000, 1500)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.register_shape('iss2.gif')
screen.bgpic('world_map.png')
iss = turtle.Turtle()
iss.shape('iss2.gif')
iss.setheading(90)
iss.penup()
iss.goto(lon, lat) # I get the error here
tkinter.mainloop()
错误信息:
Traceback (most recent call last):
File "C:/Users/Ouch/PycharmProjects/Learning/Space_station.py", line 47, in <module>
iss.goto(lon, lat)
File "C:\Python37\lib\turtle.py", line 1776, in goto
self._goto(Vec2D(x, y))
File "C:\Python37\lib\turtle.py", line 3165, in _goto
diff = (end-start)
File "C:\Python37\lib\turtle.py", line 262, in __sub__
return Vec2D(self[0]-other[0], self[1]-other[1])
TypeError: unsupported operand type(s) for -: 'str' and 'float'
错误提示您无法从字符串中减去浮点数。
因此,问题与某些值是浮点数或某些浮点数是负值无关。您不能从字符串中减去 int,或从字符串中减去正浮点数,或从字符串中减去任何其他内容。问题是您的某些值是字符串。
如果您打印出值的 repr 而不是直接打印值,您会看到这一点:
print('latitude: ', repr(lat))
print('longitude: ', repr(lon))
您会看到类似这样的内容:
latitude: '-10.4958'
longitude: '-172.9960'
因此,要解决此问题,只需将这些字符串转换为浮点数即可:
lat = float(location['latitude'])
lon = float(location['longitude'])
我正在尝试使用 "turtle" 在世界地图上显示国际 Space 站 (ISS) 的位置。我从 API 中获取了经度和纬度。然后将坐标保存到变量"lon"和"lat".
但是当我使用 iss.goto(lon, lat)
时,我收到一个 TypeError。我相信这是由于经度和纬度坐标有时是负数造成的,所以浮点数以“-”为前缀。
谁能帮我解决这个问题?
import tkinter
import turtle
import json
import urllib.request
url = 'http://api.open-notify.org/iss-now.json'
response = urllib.request.urlopen(url)
result = json.loads(response.read())
location = result['iss_position']
lat = (location['latitude'])
lon = (location['longitude'])
print('latitude: ', lat)
print('longitude: ', lon)
screen = turtle.Screen()
screen.setup(3000, 1500)
screen.setworldcoordinates(-180, -90, 180, 90)
screen.register_shape('iss2.gif')
screen.bgpic('world_map.png')
iss = turtle.Turtle()
iss.shape('iss2.gif')
iss.setheading(90)
iss.penup()
iss.goto(lon, lat) # I get the error here
tkinter.mainloop()
错误信息:
Traceback (most recent call last):
File "C:/Users/Ouch/PycharmProjects/Learning/Space_station.py", line 47, in <module>
iss.goto(lon, lat)
File "C:\Python37\lib\turtle.py", line 1776, in goto
self._goto(Vec2D(x, y))
File "C:\Python37\lib\turtle.py", line 3165, in _goto
diff = (end-start)
File "C:\Python37\lib\turtle.py", line 262, in __sub__
return Vec2D(self[0]-other[0], self[1]-other[1])
TypeError: unsupported operand type(s) for -: 'str' and 'float'
错误提示您无法从字符串中减去浮点数。
因此,问题与某些值是浮点数或某些浮点数是负值无关。您不能从字符串中减去 int,或从字符串中减去正浮点数,或从字符串中减去任何其他内容。问题是您的某些值是字符串。
如果您打印出值的 repr 而不是直接打印值,您会看到这一点:
print('latitude: ', repr(lat))
print('longitude: ', repr(lon))
您会看到类似这样的内容:
latitude: '-10.4958'
longitude: '-172.9960'
因此,要解决此问题,只需将这些字符串转换为浮点数即可:
lat = float(location['latitude'])
lon = float(location['longitude'])