0

I am trying to make an array from a loop and I keep getting 0s printed rather then the changes values, there are a few float comments from earlier in the program that are used in the function but I think its a typo as to why it isn't working.

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)

These are my two loops and arrays.

1

3 Answers 3

2

You initialize i=0 and in your while loop you check i>0. Therefore you don't enter the while loop and skip it. The same thing happens with b in the second loop

Sign up to request clarification or add additional context in comments.

6 Comments

I'm sorry I understand what you are saying but I'm not sure what I should do instead of this. Could you give an example?
you wrote i=0 then in your while loop you check i>0 and and 0<max_chase_time. Since 0>0 is False you don't enter the while loop and skip it. if you will run it with i>=0 you will enter the loop (but never exit it since max_chase_time doesn't change and i is always >=0
ok so i changed it to 'while b>=0 and 0<max_chase_time:' and got the error IndexError: index 55020 is out of bounds for axis 0 with size 55020, is that because the loop is never ending or because of something else?
Like I said you don't exit the loop. therefore your i variable will get bigger each iteration. at some point i>len(pred_position) and you will receive an IndexError
Is there a way to stop this? I'm sorry, I'm new to python.
|
0

Try pred_position.append(i+j*0.1)

1 Comment

Please try adding more detail to your responses.
0

i think you could write it like this

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.