Python中Zeno悖论的射箭示例

2024-06-01 09:37:38 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是我要模拟的Zeno's paradox

def zenos_paradox(archer_position, target_position, steps):
    current = archer_position
    print("The arrow is released at position ", archer_position)
    print("It is aimed at postion ", target_position)
    for step in range(steps):
        current = current + (target_position / 2) #change this to be 
        print("Position of the arrow at step ", step+1, "is ", current)
    return(current)

def question_2():
    answer = zenos_paradox(0,100,5)
    print("The answer is: ", answer)

question_2()

这是我的打印件:

The arrow is released at position  0
It is aimed at postion  100
Position of the arrow at step  1 is  50.0
Position of the arrow at step  2 is  100.0
Position of the arrow at step  3 is  150.0
Position of the arrow at step  4 is  200.0
Position of the arrow at step  5 is  250.0
The answer is:  250.0

我想要的打印输出:

The arrow is released at position 0
It is aimed at position 100
Position of the arrow at step 1 is 50.0
Position of the arrow at step 2 is 75.0
Position of the arrow at step 3 is 87.5
Position of the arrow at step 4 is 93.75
Position of the arrow at step 5 is 96.875
The answer is:  96.875

我知道这个位置每一步都要增加一个距离,之前增加的距离除以2。所以第一次跳跃的距离是50米,然后是25米,然后是12.5米,然后是6.25米。我认为我应该在某个地方添加一行代码,但我不知道如何在代码中使用它


Tags: oftheanswertargetissteppositioncurrent
3条回答

current + (target_position / 2)没有任何意义,因为当您要在目标位置和当前位置之间添加一半距离时,每一步都要在目标位置和起点之间添加一半距离。因此,它是current = current + (target_position - current) / 2,可以用数学简化为只取平均值:current = (current + target_position) / 2

def zenos_paradox(archer_position, target_position, steps):
    current = archer_position
    print("The arrow is released at position ", archer_position)
    print("It is aimed at postion ", target_position)
    for step in range(steps):
        current = (current + target_position) / 2
        print("Position of the arrow at step ", step+1, "is ", current)
    return(current)

def question_2():
    answer = zenos_paradox(0,100,5)
    print("The answer is: ", answer)

question_2()

您需要从目标位置减去当前位置,然后除以2,而不是目标位置本身

def zenos_paradox(archer_position, target_position, steps):
    current = archer_position
    print("The arrow is released at position ", archer_position)
    print("It is aimed at postion ", target_position)
    for step in range(steps):
        current = current + (target_position - current) / 2
        print("Position of the arrow at step ", step+1, "is ", current)
    return(current)

将新值target_position除以2后,应保存该值。所以不是

current = current + (target_position / 2)

试试这个:

target_position /= 2
current = current + target_position

完整代码:

def zenos_paradox(archer_position, target_position, steps):
    current = archer_position
    print("The arrow is released at position ", archer_position)
    print("It is aimed at postion ", target_position)
    for step in range(steps):
        target_position /= 2
        current = current + target_position #change this to be 
        print("Position of the arrow at step ", step+1, "is ", current)
    return(current)

def question_2():
    answer = zenos_paradox(0,100,5)
    print("The answer is: ", answer)

question_2()

相关问题 更多 >