Python 我的数组没有存储while循环中的值
我试图从一个循环中生成一个数组,我一直在打印0,而不是更改值。在函数中使用了程序早期的一些浮点注释,但我认为这是一个输入错误,说明了为什么它不起作用Python 我的数组没有存储while循环中的值,python,arrays,loops,while-loop,Python,Arrays,Loops,While Loop,我试图从一个循环中生成一个数组,我一直在打印0,而不是更改值。在函数中使用了程序早期的一些浮点注释,但我认为这是一个输入错误,说明了为什么它不起作用 print() pred_position = zeros(int(600*max_chase_time)) i = 0 j = speedofpredator while i>0 and 0<max_chase_time: pred_position[i] = i + j*0.1 i = i + 1 print(pre
print()
pred_position = zeros(int(600*max_chase_time))
i = 0
j = speedofpredator
while i>0 and 0<max_chase_time:
pred_position[i] = i + j*0.1
i = i + 1
print(pred_position)
print()
prey_positions = zeros(int(600*max_chase_time))
b = 0
j = speedofpredator
while b>0 and 0<max_chase_time:
prey_positions[b] = b + initpositions + j*0.1
b = b + 1
print(prey_positions)
print()
pred_位置=零(整数(600*max_chase_time))
i=0
j=前置器的速度
当i>0、00和0时,初始化i=0
,并在while循环中检查i>0
。
因此,您不会进入while循环并跳过它。
在第二个循环中,b
也会发生同样的情况,初始化i=0
,在while循环中检查i>0
。
因此,您不会进入while循环并跳过它。
第二个循环中的b
也会发生同样的情况尝试pred\u position.append(i+j*0.1)尝试pred\u position.append(i+j*0.1)我想你可以这样写
max_chase_time = 600 #secs
speed_of_predator = 30 #meter per seconds
speed_of_prey = 10 #meter per seconds
pred_pos = 0 #pred original position
prey_pos = 5000 #prey original position
current_time = 0 #Current time
prey_is_alive = True #Current prey status
while current_time < max_chase_time:
current_time += 1 # increment current_time
pred_pos += speed_of_predator # increment pred_pos
prey_pos += speed_of_prey # increment prey_distance
print(current_time, pred_pos, prey_pos)
if (prey_pos - pred_pos) <= 0: # if distance is less or equal 0
prey_is_alive = False # the prey is eaten
break # exit loop if prey is dead
print("Alive", prey_is_alive)
max_chase_time=600秒
捕食者的速度=每秒30米
猎物的速度=10米/秒
pred_pos=0#pred原始位置
猎物位置=5000#猎物原始位置
当前时间=0#当前时间
猎物_是_活着=真实#当前猎物状态
当当前时间<最大追逐时间:
当前_时间+=1#增加当前_时间
pred_pos+=捕食者的速度#增加pred_pos
猎物位置+=猎物的速度#增加猎物距离
打印(当前时间、预测位置、预测位置)
如果(prey_pos-pred_pos)我想你可以这样写
max_chase_time = 600 #secs
speed_of_predator = 30 #meter per seconds
speed_of_prey = 10 #meter per seconds
pred_pos = 0 #pred original position
prey_pos = 5000 #prey original position
current_time = 0 #Current time
prey_is_alive = True #Current prey status
while current_time < max_chase_time:
current_time += 1 # increment current_time
pred_pos += speed_of_predator # increment pred_pos
prey_pos += speed_of_prey # increment prey_distance
print(current_time, pred_pos, prey_pos)
if (prey_pos - pred_pos) <= 0: # if distance is less or equal 0
prey_is_alive = False # the prey is eaten
break # exit loop if prey is dead
print("Alive", prey_is_alive)
max_chase_time=600秒
捕食者的速度=每秒30米
猎物的速度=10米/秒
pred_pos=0#pred原始位置
猎物位置=5000#猎物原始位置
当前时间=0#当前时间
猎物_是_活着=真实#当前猎物状态
当当前时间<最大追逐时间:
当前_时间+=1#增加当前_时间
pred_pos+=捕食者的速度#增加pred_pos
猎物位置+=猎物的速度#增加猎物距离
打印(当前时间、预测位置、预测位置)
如果(prey_pos-pred_pos),很抱歉,我理解你的意思,但我不确定我应该做什么来代替这个。你能举个例子吗?你写了i=0,然后在while循环中检查i>0,00为False,你不进入while循环并跳过它。如果你在i>=0的情况下运行它,你将进入循环(但永远不会退出它,因为max\u chase\u time不会改变,而且我总是>=0 OK,所以我将它改为'while b>=0和0,就像我说的那样,你不会退出循环。因此每次迭代你的i变量都会变大。在某个点i>len(pred\u位置)你会收到一个索引器。有什么方法可以阻止它吗?很抱歉,我是python新手。很抱歉,我理解你的意思,但我不确定我应该做什么来代替它。你能举个例子吗?你写了I=0,然后在你的while循环中检查I>0,00为False,你不会进入while循环并跳过它。如果你要运行它,请当i>=0时,你将进入循环(但永远不会退出循环,因为max\u chase\u time不会改变,而且i始终>=0,所以我将它改为“而b>=0,就像我说的那样,你不会退出循环。因此每次迭代你的i变量都会变大。在某个点i>len(pred\u位置)您将收到一个索引器。是否有办法停止此操作?很抱歉,我是python新手。请提供,以及当前和预期的输出。是否进行了任何调试?我建议阅读。请提供,以及当前和预期的输出。是否进行了任何调试?我建议阅读。请尝试添加请为您的答复添加更多详细信息。请尝试为您的答复添加更多详细信息。