有人可以帮我这个代码吗?当前它显示错误:
ValueError:无效的转换规范
我不确定如何解决。另外,您能否告诉我如何正确设置此代码的格式,使其看起来更整洁,以及我是否做错了其他事情或是否会出现其他错误?
为了进行测试,如果需要,可以使用两组经度和纬度:
Latitude: 17.935667
Longitude: -76.7875
和
Latitude: -34.945
Longitude: 138.530556
这是我的代码:
import math
radius = 6371.01
city1 = raw_input("First city: ")
x1 = float(input("First latitude: "))
y1 = float(input("First longitude: "))
city2 = raw_input("Second City: ")
x2 = float(input("Second latitude: "))
y2 = float(input("Second longitude: "))
def distance(radius, x1, x2, y1, y2):
d = radius * math.acos(math.sin(x1)*math.sin(x2)+math.cos(x1)*math.cos(x2)*math.cos(abs(y1-y2)))
return d
distance = distance(radius, x1, x2, y1, y2)
Blackbird = 3230
BombardierCRJ1000 = 870
tbird = distance/3230
tBombardierCRJ1000 = distance/870
cseconds = tBombardierCRJ1000 * 60 * 60 * 60
hctime = cseconds / 3600
cseconds -= 3600*hctime
mctime = cseconds / 60
hseconds = tbird * 60 * 60 * 60
hhtime = hseconds / 3600
hseconds -= 3600*hhtime
mhtime = hseconds / 60
print("Commercial aircraft: Bombardier CRJ 1000 - 870 km/hr")
print("High-Speed aircraft: SR-71 Blackbird - 1354 km/hr")
print("Departure")
print("City Latitude Longitude")
print("{0:s} {1:.5f} - {2:.5f)}" .format(city1, x1, y1))
print("--------------------------------------------------------------- -------")
print("Destination")
print("City Latitude Longitude Distance Commercial High-Speed")
print(" (km) Time (h:m) Time (h:m)")
print("{0:s} {1:.5f} {2:.5f} {3:.2f} {4:d}:{5:d} {6:d}:{7:d}" .format(city2, x2,y2, distance, hctime,mctime, hhtime, mhtime))
最佳答案
格式化中存在语法错误。您应该从)
中删除{2:.5f)}
:
print("{0:s} {1:.5f} - {2:.5f}".format(city1, x1, y1)),
关于python - Python飞行距离和时间计算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31901419/