-2

Hey guys I want the list that the user is creating with input to print in the order from latest entry to first entry..

the code I have right now does it for a few posts then it prints the list in some weird order

all_status_updates = []


def status():
    while True: 
        status = input("Type Status Update Here...\n")
        if status != "exit":
            all_status_updates.append(status)
            all_status_updates.sort(reverse = True) 
            print(all_status_updates)
        elif status == "exit":
            break
status()

hauntboy
  • 15
  • 5
  • 1
    Can you please show us the output? Are you confused by sorting strings? – Fourier Dec 17 '19 at 09:38
  • 1
    Does this answer your question? [How can I reverse a list in Python?](https://stackoverflow.com/questions/3940128/how-can-i-reverse-a-list-in-python) – Georgy Dec 17 '19 at 10:11

2 Answers2

0

When you sort, it will put the inputs in alphabetical order. If you just want to reverse the list, use myList.reverse() EDIT : reverse only at the end to have the expected result.

all_status_updates = []


def status():
    while True: 
        status = input("Type Status Update Here...\n")
        if status != "exit":
            all_status_updates.append(status)
            print(all_status_updates)
        elif status == "exit":
            break
    all_status_updates.reverse()
    print(all_status_updates)

status()
Phoenixo
  • 2,071
  • 1
  • 6
  • 13
-2

Several things, which might not work in your example:

  • You do not need to sort(), but to reverse the order of your list. For this you can use list slicing
  • You should not name the variable for user input status, which is the same name as the function
def status():

    all_status_updates = []
    while True: 
        stat = input("Type Status Update Here...\n")
        if stat != "exit":
            all_status_updates.append(stat)
            print(all_status_updates[::-1])
        elif stat == "exit":
            break

status()

OUTPUT

Type Status Update Here...
a
['a']
Type Status Update Here...
b
['b', 'a']
Type Status Update Here...
c
['c', 'b', 'a']
Type Status Update Here...
d
['d', 'c', 'b', 'a']
Type Status Update Here...
e
['e', 'd', 'c', 'b', 'a']
Type Status Update Here...
f
['f', 'e', 'd', 'c', 'b', 'a']
Type Status Update Here...
exit
AnsFourtyTwo
  • 2,480
  • 2
  • 13
  • 33
  • This does not change anything? – Fourier Dec 17 '19 at 09:38
  • thank you so much Simon! and will do thanks again for all the help – hauntboy Dec 17 '19 at 09:38
  • Did you read the question ? "I want the list that the user is creating with input to print in the order from latest entry to first entry". – bruno desthuilliers Dec 17 '19 at 09:40
  • @hauntboy this does not answer your question. And it will actually make no difference (except for saving a couple cpu cycles). – bruno desthuilliers Dec 17 '19 at 09:41
  • I'm very sorry, I was so convinced my solution would work, that I didn't test it. Now I did and now it should show the right behaviour. – AnsFourtyTwo Dec 17 '19 at 09:57
  • thanks but Simon I want the list to print after every entry not just at the end yours works but without printing after every entry which is not what I'm going for – hauntboy Dec 17 '19 at 10:02
  • In this case, you cannot use `reverse()`, as it reverts the list inplace. You need to use list slicing in this case. I'll edit. – AnsFourtyTwo Dec 17 '19 at 10:05
  • that did it weird I tried the slicing and it did not work for me earlier because I was adding the slicing in the wrong place thanks Simon! appreciate it – hauntboy Dec 17 '19 at 10:10