简体   繁体   中英

My array is not storing values from the while loop

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.

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

尝试 pred_position.append(i+j*0.1)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM