0
import sys
import math
import random

def main():
    # your code goes here
    
    j = [1,1,2,3,5]
    GMDN(j)
    
    
def GMDN(x):

    n = 5
    x = [1,1,2,3,5]
    arithmetic = (x[0] + x[1] + x[2] + x[3] + x[4])/n
    geometric = (x[0] * x[1] * x[2] * x[3] * x[4])**(1./n)
    median = x[int((n)/2)]
    my_list = [arithmetic,geometric,median]
    my_list.sort()
    print(my_list)
    while(my_list[0] != my_list[1] and my_list[1] != my_list[2]):
        n1 = (my_list[0] + my_list[1] + my_list[2])/3
        n2 = (my_list[0] * my_list[1] * my_list[2])**(1./3)
        n3 = my_list[int((3)/2)]
        my_list = [n1,n2,n3]
        my_list.sort()
        print(my_list)
        

if __name__ == '__main__':
    main()

I'm trying to write a code that uses a while loop with more than one condition but whenever I run the code it doesn't process both conditions and only does one. Anyone know why this happens?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
Popthrop
  • 1
  • 1
  • 3
    If the first condition of `and` is false, there is no need to evaluate the second condition and so it doesn't. Perhaps you want `or` instead. – chepner Jan 10 '22 at 17:04
  • What specific test are you trying to make with those conditions? – ekhumoro Jan 10 '22 at 17:53
  • Are you waiting for all three values to converge? That is, in fact, `my_list[0] != my_list[1] or my_list[1] != my_list[2]`, or using De Morgans's laws and comparison chaining, `not (my_list[0] == my_list[1] == my_list[2])`. – chepner Jan 11 '22 at 16:41

1 Answers1

-1

while(my_list[0] != my_list[1] and my_list[1] != my_list[2]): this code will process both conditions.

what I think you are missing is that you are using static values. my_list[0] will always return the first item in the series. if you want to reach another element in your series whenever you go through the iteration you need to use dynamic values. if you are intentionally using static values as that might be the case based on your calculations in the iteration, then most likely your calculations are not working as you expect.

EDIT: based on the above comments, if you meant one or the other condition you need to use or instead of and.