Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 记住迭代次数的循环的条件_Python_Iteration_Conditional - Fatal编程技术网

Python 记住迭代次数的循环的条件

Python 记住迭代次数的循环的条件,python,iteration,conditional,Python,Iteration,Conditional,我正在尝试编写一个包含两个输入和一个输出的类。该类每秒调用10次。其中一个输入是不随时间变化的列表,但另一个输入随时间变化 此类的输出是输入列表的成员。 这个班级应该是这样的 import lib601.sm as sm counter = 0 class FollowFigure(sm.SM): def __init__(self, waypointsList): self.waypoints = waypointsList def getNextValues

我正在尝试编写一个包含两个输入和一个输出的类。该类每秒调用10次。其中一个输入是不随时间变化的列表,但另一个输入随时间变化

此类的输出是输入列表的成员。 这个班级应该是这样的

import lib601.sm as sm

counter = 0
class FollowFigure(sm.SM):
    def __init__(self, waypointsList):
        self.waypoints = waypointsList
    def getNextValues(self, state, inp):
        global counter
        sensors = inp
        current_point = sensors.odometry.point() 

        if counter == 0:
            while not(self.waypoints[0].isNear(current_point, 0.01)):
                return (state, self.waypoints[0])
            counter += 1
        if counter == 1:
            while not(self.waypoints[1].isNear(current_point, 0.01)):
                return (state, self.waypoints[1])
            counter += 1
        if counter == 2:
            while not(self.waypoints[2].isNear(current_point, 0.01)):
                return (state, self.waypoints[2])
            counter += 1                
        if counter == 3:
            while not(self.waypoints[3].isNear(current_point, 0.01)):
                return (state, self.waypoints[3])
因此,类必须检查一个随时间变化的条件,如果该条件不满足,则返回inp[0],并等待它满足,而不检查其他条件。如果inp1[0]的条件满足,则转到下一个while,并且在下一次调用此类时,不要检查上一个while。在过去的5个小时里,我一直在思考这个问题,但不知道如何解决它。应该没那么难。也许我不能很好地集中注意力

我曾尝试为此编写一个for循环,但由于该类在某些情况下每秒调用10次,因此它没有应有的输出,因为每次调用时都会重置迭代。顺便说一句,这个类应该适用于任何有限长度inp1


编辑:上述代码适用于包含4个元素的输入列表。

您似乎正在编写一个应用程序,通过每0.1秒检查一次位置来检测是否有人按顺序通过指定的航路点

在下面的代码中,我假设每次调用函数时,它都会返回要访问的下一个检查点。因此,开始时它将返回
航路点[0]
,稍后当我们到达
航路点[0]
,函数将返回
航路点[1]
,直到我们到达
航路点[2]

您的代码可以大大简化为:

import lib601.sm as sm

class FollowFigure(sm.SM):
    def __init__(self, waypointsList):
        self.waypoints = waypointsList
        self.next_to_visit = 0  # The next waypoint to be visited

    def getNextValues(self, state, inp):
        sensors = inp
        current_point = sensors.odometry.point()
        if self.waypoints[self.next_to_visit].isNear(current_point, 0.01):
            # We reached the next waypoint, go for next waypoint
            self.next_to_visit += 1
        # Return the next waypoint to be visited
        return (state, self.waypoints[self.next_to_visit])
请注意,函数中没有
while
循环。
return
语句将始终返回
waypoints[0]
,而我们尚未到达
waypoints[0]
,因为
self.next to\u visit
将始终为0,直到我们到达
waypoints[0]
。因此,在任何函数调用中返回
航路点[self.next\u to\u visit]
都是正确执行所需任务的过程。=)


然后,在我们增加
self.next\u to\u visit
后,您可以通过检查
self.next\u to\u visit
的值是否等于
waypoints
列表的长度来检查是否已完成所有航路点。如果相等,那么我们已经到达了最终目的地,您可以相应地返回物品。

您似乎正在编写一个应用程序,通过每0.1秒检查一次位置来检测是否有人按顺序通过指定的航路点

在下面的代码中,我假设每次调用函数时,它都会返回要访问的下一个检查点。因此,开始时它将返回
航路点[0]
,稍后当我们到达
航路点[0]
,函数将返回
航路点[1]
,直到我们到达
航路点[2]

您的代码可以大大简化为:

import lib601.sm as sm

class FollowFigure(sm.SM):
    def __init__(self, waypointsList):
        self.waypoints = waypointsList
        self.next_to_visit = 0  # The next waypoint to be visited

    def getNextValues(self, state, inp):
        sensors = inp
        current_point = sensors.odometry.point()
        if self.waypoints[self.next_to_visit].isNear(current_point, 0.01):
            # We reached the next waypoint, go for next waypoint
            self.next_to_visit += 1
        # Return the next waypoint to be visited
        return (state, self.waypoints[self.next_to_visit])
请注意,函数中没有
while
循环。
return
语句将始终返回
waypoints[0]
,而我们尚未到达
waypoints[0]
,因为
self.next to\u visit
将始终为0,直到我们到达
waypoints[0]
。因此,在任何函数调用中返回
航路点[self.next\u to\u visit]
都是正确执行所需任务的过程。=)


然后,在我们增加
self.next\u to\u visit
后,您可以通过检查
self.next\u to\u visit
的值是否等于
waypoints
列表的长度来检查是否已完成所有航路点。如果相等,那么我们已经到达了最终目的地,您可以相应地返回物品。

您需要返回并重写整个方法以实现您的目标。你的代码是我见过的最差的。你从不以程序化的方式打电话。不确定为什么要重复返回相同的值,而您可以创建一个函数来执行一次。底线是,您需要重写。保留一个通过条件的项目列表,然后在每次迭代中检查原始
inp1
中不在“已检查”列表中的项目。我真正不理解的是为什么
呢?如果该条件从未满足,程序将处于无限循环中。@RichardGrant如果我知道如何编写该函数,我就会编写它。请注意,所有那些
while
语句实际上都是
If
语句,因为里面有
return
语句。因此,它将总是在最多一次迭代中打破循环(如果while循环中的条件未满足,则可以为0)。您需要返回并重写整个方法以实现您的目标。你的代码是我见过的最差的。你从不以程序化的方式打电话。不确定为什么要重复返回相同的值,而您可以创建一个函数来执行一次。底线是,您需要重写。保留一个通过条件的项目列表,然后在每次迭代中检查原始
inp1
中不在“已检查”列表中的项目。我真正不理解的是为什么
呢?如果该条件从未满足,程序将处于无限循环中。@RichardGrant如果我知道如何编写该函数,我就会编写它。请注意,所有那些
while
语句实际上都是
If
语句,因为里面有
return
语句。因此,它将总是在最多一次迭代中打破循环(如果while循环中的条件未满足,则可以为0)。是的,这正是我想要的,它可以工作。谢谢我想我在OOP中没有理解很多东西。您已经在init方法中定义了self.next_to_visit属性。据我所知,每当调用类时,方法init也将