Home > Software design >  Python - Variable in loop not keeping value
Python - Variable in loop not keeping value

Time:09-28

SOLVED!

I'm trying to find the highest value of a list (certain restrictions apply). I assigned the value to a variable within a for loop but when trying to access it outside I cant. My output remains 0.

Any help/suggestions?

'''

    highestDiff = 0
    sizeOfList = 6

    for i in range(sizeOfList, sizeOfList - 1):
        #   Find the difference values
        tempDifference = abs(listOfFloats[i 1] - listOfFloats[i])
        #   Find the highest difference
        if tempDifference > highestDiff:
            highestDiff = tempDifference
    #   Output the highest difference
    print("The highest difference between values is ", highestDiff)

'''

Edit: I had sizeOfLoop declared at 6. This wasn’t included originally as I didn’t copy that part of the code here.

Solution: So it seems by changing my range from (sizeOfList, sizeOfList-1) to just (sizeOfList - 1) has fixed my issue. Thanks for the quick replies everyone!

CodePudding user response:

There's a bug with the range you're using to loop through the list. Pretty easy to fix bug, that can easily slip unnoticed.

Here's a simple fix, as also suggested by chepner:

import numpy as np

highestDiff = 0
sizeOfList = 10
listOfFloats = np.random.random(sizeOfList).tolist()

for i in range(sizeOfList - 1):
    # Find the difference values
    tempDifference = abs(listOfFloats[i 1] - listOfFloats[i])
    print(tempDifference)
    
    # Find the highest difference
    if tempDifference > highestDiff:
        highestDiff = tempDifference

#   Output the highest difference
print("The highest difference between values is", highestDiff)

Another approach that iterates directly through the list:

import numpy as np

highestDiff = 0
sizeOfList = 10
listOfFloats = np.random.random(sizeOfList).tolist()

for left, right in zip(listOfFloats[0:-1], listOfFloats[1:]):
    # Find the difference values
    tempDifference = abs(right - left)
    print(tempDifference)
    
    # Find the highest difference
    if tempDifference > highestDiff:
        highestDiff = tempDifference

#   Output the highest difference
print("The highest difference between values is", highestDiff)

Here's a one-liner sollution:

max(map(lambda value: abs(value[1] - value[0]), zip(listOfFloats[:-1], listOfFloats[1:])))

CodePudding user response:

So it seems by changing my range from (sizeOfList, sizeOfList-1) to just (sizeOfList - 1) has fixed my issue. Thanks for the quick replies everyone!

CodePudding user response:

I suspect the challenge is coming from the range function. This is telling i to start at the size of the list and end at an even smaller number. If there are 7 values in the list this is saying to start i at 7 and to then go to 6, but a -1 step also has not been specified. I would try printing a statement at the beginning of the loop so you can tell how many iterations it is running for, I believe it is only running once if at all.

If the highest difference is just the maximum possible distance between two numbers in the set then you could try something along these lines:

    largest = list[0] #By setting to a number in the list we avoid 
    smallest = list[0]#logical errors, what if the lowest # is 
                      #more than 0?
    for x in list:
      if x > largest:
        largest = x
      if x < smallest:
        smallest = x
    highestDiff = largest - smallest
    

It seems your code may be only comparing the difference of two adjacent indexes. Is this intended? What restrictions apply that you must account for?

  • Related