Python program to specify Number of integers and Sort them in Ascending order

Python program to specify Number of integers and Sort them in Ascending order

Below is the Python program that given a list of integers, SORTS them in Ascending Order (smallest to largest 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 enter to Sort them in Ascending order?: ")

if upperLimit.isdigit() == True:

    upperLimit = int(upperLimit) 

    print("Please enter {} integers to sort them in Ascending 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()

        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 Ascending 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 Ascending order
Python | thetqweb