0

I'm trying to write a program that takes a list given by a user, checks if it is/isn't in decreasing order and print out an appropriate statement. The program works okay if I input values which are/aren't in decreasing order such as [5,4,3,2,1] or [1,2,3,4,5]. However, if I input something like [5,4,5,4,3,2,1] or [1,2,1,2,3,4,5] it will still say the list is/isn't in decreasing order. I imagine it's because the way I have my code written it's only comparing the first item in the list to the second, or something like that. But I haven't for the life of me being able to figure out how to compare each item in the list to the next so that the program is accurate.

def decreasingOrder():
    element = 0
    integer_list = []

    userInput = input("Please enter your numbers seperated by a comma (,):")
    inputtedStrings = userInput.split(",")
    for number in inputtedStrings:
        inputtedIntegers = int(number)
        integer_list.append(inputtedIntegers)

    if integer_list[element] > integer_list[element-1]:
        print("The list is in decreasing order.")
    else:
        print("The list is not in decreasing order.") 

    
        

decreasingOrder()

    
            

That's the code. As previously stated, the program should print "The list is in decreasing order." if the list is in decreasing order, and "The list is not in decreasing order." if the list isn't.

  • You need to iterate over the list, not just compare the first element. `for ix, val in enumerate(integer_list[:-1]): if val <= integer_list[ix+1]; return False` or something like that – NickHilton Dec 07 '22 at 21:34
  • As the question states, this is not a yes no question. Seems 3rd option needs to be considered. That is `[5,4,5,4,3,2,1] or [1,2,1,2,3,4,5]` are neither descending or ascending. Maybe add an elif. – Carl_M Dec 07 '22 at 21:35
  • Missed that, `increasing=True; decreasing=True; for ix, val in enumerate(integer_list[:-1]): if val <= integer_list[ix+1]; decreasing=False; if val >= integer_list[x+1]; increasing=False;` should check both cases. Side note; I wouldn't call the function 'decreasingOrder` as its a misleading name – NickHilton Dec 07 '22 at 22:04
  • Does this answer your question? [How can I iterate over overlapping (current, next) pairs of values from a list?](https://stackoverflow.com/questions/5434891/how-can-i-iterate-over-overlapping-current-next-pairs-of-values-from-a-list) – mkrieger1 Dec 07 '22 at 22:15

2 Answers2

0

A different approach. Use the sorted function and test for increasing or decreasing.

def decreasingOrder():
    element = 0
    integer_list = []
    userInput = input("Please enter your numbers seperated by a comma (,):")
    inputtedStrings = userInput.split(",")
    for number in inputtedStrings:
        inputtedIntegers = int(number)
        integer_list.append(inputtedIntegers)
    increasing = integer_list == sorted(integer_list)
    decreasing = integer_list == sorted(integer_list, reverse=True)

    if decreasing:
        print("The list is in decreasing order.")
    elif increasing:
        print("The list is not in decreasing order.")
    else:
        print("The list is mixed neither increasing or decreasing order.")


decreasingOrder()
Carl_M
  • 861
  • 1
  • 7
  • 14
-1

You can use the zip() function to check each element with its successor and determine if they are all greater (which means the list is in decreasing order):

list1 = [100,50,10,5,1]

if all(a>b for a,b in zip(list1,list1[1:])):
    print("list1 is in decreasing order")     # <--
else:
    print("list1 is not in decreasing order")

list2 = [100,5,10,5,1]

if all(a>b for a,b in zip(list2,list2[1:])):
    print("list2 is in decreasing order")
else:
    print("list2 is not in decreasing order") # <--
Alain T.
  • 40,517
  • 4
  • 31
  • 51