Python program to specify Number of integers and Sort them in Descending order
Python program to specify Number of integers and Sort them in Descending order
Below is the Python program that given a list of integers, SORTS them in Descending Order (largest to smallest in a comma-separated list). The program expects a positive Integer as the Input to the “Number of integers to enter“, and exits gracefully with a custom message “The value you entered is a non-Integer OR -ve Integer!” otherwise. The program also checks if the numbers entered are positive integers and rejects any other type, requesting the user for a positive Integer instead!
#!/usr/bin/python3
numberCount = 0
total =0
average = 0
integerList = []
n = 0
greatestNumber = 0
upperLimit = input("How many integers do you want to enter to Sort them in Descending order?: ")
if upperLimit.isdigit() == True:
upperLimit = int(upperLimit)
print("Please enter {} integers to sort them in Descending order!\n\n".format(upperLimit))
while numberCount < upperLimit:
number = input("Enter number:")
try:
number = float(number)
if number.is_integer():
number = int(number)
integerList.append(number)
numberCount += 1
integerList.sort(reverse = True)
except:
print("You entered a non-Integer, instead enter a +ve/-ve Integer!")
print("\n\nYou entered {} integers and this is the list in a Descending order: ".format(upperLimit) + str(integerList) + "!")
else:
print("The value you entered is a non-Integer OR -ve Integer!")
Python program to specify Number of integers and Sort them in Descending order
Python | thetqweb