简体   繁体   中英

Python 3 Largest to smallest loop

I am attempting to write a program that asks a user to enter a series of numbers. Once the numbers are entered they should be ordered from largest to smallest. Here is what I have so far, I am having a little trouble figuring out how I can order the numbers the user has put in. I'm pretty sure a do-while loop should be used, and I am trying to figure out how to implement it. Also the user should enter -99 to end the series of numbers.

   //Define main function
    def main():
        numbers()
        loop()


    //Get user input
    def numbers():
        a=input()
        b=input()
        c=input()
        d=input()
        e=input()
        number1=int(a)
        number2=int(b)
        number3=int(c)
        number4=int(d)
        number5=int(e)

    //Define loop function
    def loop():

    //Call main function
    main()

I perhaps shouldn't be helping you to this extent, learning and all, but I am utterly unable to resist posting this oneliner:

def thingy():
    return sorted(map(int, iter(raw_input, '-99')), reverse=True)

Maybe something like this:

#! /usr/bin/python3
numbers = []
while True:
    number = int(input('Give me a number: '))
    if number == -99: break
    numbers.append(number)
for number in sorted(numbers, reverse=True):
    print(number)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM