0

is there any way to know if the previous appended value is greater than the newly appended value using while loop

unifarr = []
forwarr = []
while t :
unif = randint(-75, 75)  # To move face in every possible angle
forw = randint(-1, 1)  # Move every 1 pixel, -1 for back 1 for forward
t.left(unif)
t.forward(forw)
sleep(0.01)
forwarr.append(forw) 
unifarr.append(unif)# check if prev. value is < or > than newly appended value

i am using random that appends in an array is there any possible way to check if the previous appended random value is greater than the newly appended value

2
  • Please provide the complete code Commented Feb 13, 2021 at 18:29
  • assume forwarr is a list, you could do if forwarrr[-1] > forw Commented Feb 13, 2021 at 18:35

2 Answers 2

1

You can create your base case outside while loop and check whether the newly created number is greater than the base case or not. If greater, you do your stuff and finally set your previous to new number for future iterations. Something like this would do the job:

from random import randint

previous_forv = randint(-75, 75)
previous_unif = randint(-1, 1)
forwarr = [previous_forv]
unifarr = [previous_unif]

while t:
    new_forv = randint(-75, 75)
    new_unif = randint(-1, 1)

    t.left(unif)
    t.forward(forw)
    sleep(0.01)
    
    if new_forv > previous_forv:
        forwarr.append(new_forv)
        previous_forv = new_forv

    if new_unif > previous_unif:
        unifarr.append(new_unif)
        previous_unif = new_unif
Sign up to request clarification or add additional context in comments.

Comments

1

you can index using negative number to index the list from the other end. i.e with -1 you will get the last value with -2 second last value and so on.

if unif > unifarr[-1]:
   print ("is greater")
else:
  print ("is not greater")

unifarr.append(unif)

OR

unifarr.append(unif)

if unifarr[-1] > unifarr[-2]:
   print ("is greater")
else:
  print ("is not greater")

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.