Put N values in order

Asked

Viewed 226 times

0

I need to do a show that reads n numbers (indeterminate quantity) and then put them in order. The stop condition is the number 0.

I thought I’d throw those figures into a list and then sort them out. I made a while loop, however, obviously it will only hold the last value.

  • Put your attempt to the question so you can actually figure out where you went wrong and get more targeted help.

  • Try to put the code of what you tried to do.

1 answer

0

Good afternoon, all right? Man, depending on the way you’re doing things, I’m missing the python on this computer, but I believe this code will do what you’re thinking, I hope it comes back whether it did or not, right. I’ll be waiting.

    lis_ord = []

    while True:

    num = int(input("Por favor, adicione um número a lista [insira 0 para sair]: "))

    if num > 0 or num < 0:
        lis_ord.append(num)
        lis_ord.sort
        print("O número foi adicionado com sucesso, sua lista é:")
        print(lis.ord)

    elif num == 0:
        print("Obrigado por usar o programa. Encerrando.")
        break

    else:
        print("Comando inválido. Use apenas números.")

The above code block should work as follows:

Creates a list and links it to the lis_ord variable; While true, it asks the user to provide an input, stored in the variable one, then it organizes the list from the smallest to the largest, print that the number has been added, and display the list in its current state; If the input is greater than or less than zero, it appends the list lis_ord; If the input is zero, it terminates the program; If the input is a STRING, or a FLOAT, for example, printthe invalid command, and returns to the user input;

Maybe there’s something wrong, because I couldn’t test it, but whatever, just come back with an answer and I’ll try to help again. Putting your code would also be of great help, to understand where the error is occurring. Hope I helped, hugs.

  • It worked out! Thank you so much, man. Happy holidays to you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.