Python if 语句:Unndent 不匹配任何外部缩进级别

我有以下 Python 代码


def reward_function(params):

        import math

        # Read input variables

        waypoints = params['waypoints']

        closest_waypoints = params['closest_waypoints']

        heading = params['heading']

        # Initialize the reward with typical value 

        reward = 1.0

        # Calculate the direction of the center line based on the closest waypoints

        next_point = waypoints[closest_waypoints[1]]

        prev_point = waypoints[closest_waypoints[0]]

        # Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians

        track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0]) 

        # Convert to degree

        track_direction = math.degrees(track_direction)

        # Calculate the difference between the track direction and the heading direction of the car

        direction_diff = abs(track_direction - heading)

    if direction_diff > 180: # This line gives me the ERROR

        direction_diff = 360 - direction_diff

        # Penalize the reward if the difference is too large

        DIRECTION_THRESHOLD = 10.0

        if direction_diff > DIRECTION_THRESHOLD:

        reward *= 0.5

        return reward

我尝试了几个不同的编辑器,但无法解决这个缩进问题。任何指针都非常受欢迎。


蓝山帝景
浏览 167回答 1
1回答

墨色风雨

不要使用 8 个空格(2 个制表符),使用 4 个(1 个制表符):def reward_function(params):    import math    # Read input variables    waypoints = params['waypoints']    closest_waypoints = params['closest_waypoints']    heading = params['heading']    # Initialize the reward with typical value     reward = 1.0    # Calculate the direction of the center line based on the closest waypoints    next_point = waypoints[closest_waypoints[1]]    prev_point = waypoints[closest_waypoints[0]]    # Calculate the direction in radius, arctan2(dy, dx), the result is (-pi, pi) in radians    track_direction = math.atan2(next_point[1] - prev_point[1], next_point[0] - prev_point[0])     # Convert to degree    track_direction = math.degrees(track_direction)    # Calculate the difference between the track direction and the heading direction of the car    direction_diff = abs(track_direction - heading)    if direction_diff > 180: # This line gives me the ERROR        direction_diff = 360 - direction_diff        # Penalize the reward if the difference is too large        DIRECTION_THRESHOLD = 10.0        if direction_diff > DIRECTION_THRESHOLD:            reward *= 0.5            return reward
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python