a program that asks for the user's input of a list of numbers and sort it in reverse in python 1

a program that asks for the user’s input of a list of numbers and sort it in reverse in python

# this is for retrocompatibility. Python 3 users will ditch raw_input altogether
try:
    raw_input
except NameError:
    raw_input = input   # for python 3 users 🙂

# initialize the list with the first element
my_list = [int(raw_input("Enter a number: "))]

while True:
    b = raw_input("Do you want to input more: ")
    if b == 'yes':
        # add one element
        my_list.append(int(raw_input("Enter another number:")))
    elif b == 'no':
        my_list.sort(reverse=True)
        print(my_list)
        break

Here is what the above code is Doing:
1. It asks the user to input a number.
2. It asks the user if he wants to input more numbers.
3. If the user says yes, it asks the user to input another number and adds it to the list.
4. If the user says no, it sorts the list in descending order and prints it.

Similar Posts