代码之家  ›  专栏  ›  技术社区  ›  epubby sudsou

为什么ts〔name〕的东西在python中不断出错

  •  0
  • epubby sudsou  · 技术社区  · 1 年前

    我用python做了一个卫星位置探测项目。但此代码引发了以下错误:

    Traceback (most recent call last):
      File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 50, in <module>
        calculate_and_display_position(name.upper())  # Convert to uppercase for TLE lookup
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 15, in calculate_and_display_position
        satellite = ts[name]
                    ~~^^^^^^
    TypeError: list indices must be integers or slices, not str
    

    我的代码是:

    import turtle
    from datetime import datetime
    from skyfield.api import Topos, load
    
    # Function to calculate and display satellite position
    def calculate_and_display_position(name):
        # Clear previous drawing
        screen.clear()
        
        # Load TLE data
        ts = load.tle_file('stations.txt')
        
        try:
            satellite = ts[name]
        except KeyError:
            screen.write(f"Satellite '{name}' not found.", align="center", font=("Arial", 16, "normal"))
            return
    
        # Get current UTC time
        current_time = datetime.utcnow()
        
        # Calculate satellite position
        topos = satellite.at(ts.utc(current_time.year, current_time.month, current_time.day,
                                      current_time.hour, current_time.minute, current_time.second))
        alt, az, _ = topos.observe(observer_location).apparent().altaz()
    
        # Display satellite name, position, and local time
        screen.write(f"Satellite: {name}\n"
                     f"Altitude: {alt.degrees:.2f} degrees\n"
                     f"Azimuth: {az.degrees:.2f} degrees\n"
                     f"Local Time: {current_time}", align="left", font=("Arial", 16, "normal"))
    
    # Create a Turtle screen
    screen = turtle.Screen()
    screen.bgpic("world_map.gif")  # You'll need to provide a world map image
    
    # Create an observer location
    observer_location = Topos(latitude_degrees=23.247921, longitude_degrees=87.871365)
    
    # Initialize the Turtle
    satellite_turtle = turtle.Turtle()
    satellite_turtle.penup()
    satellite_turtle.goto(0, 200)
    satellite_turtle.hideturtle()
    
    # Input box for satellite name
    name = screen.textinput("Enter Satellite Name", "Enter the satellite name:")
    if name:
        calculate_and_display_position(name.upper())  # Convert to uppercase for TLE lookup
    
    # Listen for clicks to change satellite
    screen.onclick(lambda x, y: calculate_and_display_position(screen.textinput("Enter Satellite Name", "Enter the satellite name:").upper()))
    
    # Close the window when clicked
    screen.exitonclick()
    

    现在,如果我单击“确定”按钮,那么它就会发生: The Moment When The Code Is Run, It Throws A Dialog Asking For The Name

    我试着把 ts = load.tle_file('stations.txt') 的外部 calculate_and_display_position(name) 和里面 calculate_and_display_position(名称) ,我写 global ts 但仍然显示出相同的东西。

    我正在分析一个输出,当输入最新的卫星名称时,模块将获取坐标并在地图上做一个点,并用红色显示名称的位置。

    0 回复  |  直到 1 年前