Python program to specify Number of integers and Compute the Largest

Python program to specify Number of integers and Compute the Largest

Below is the Python program that given a list of integers, computes the MAXIMUM/LARGEST number. 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/negative Integers and rejects any other type, requesting the user for a positive/negative Integer instead!

 

#!/usr/bin/python3

numberCount = 0
total =0
average = 0
integerList = []
n = 0
greatestNumber = 0
index = 0

upperLimit = input("How many integers do you want to enter to find the one with the Maximum value?: ")

if upperLimit.isdigit() == True:

    upperLimit = int(upperLimit) 

    print("Please enter {} integers to find the Maximum value! \n\n".format(upperLimit))


    while numberCount < upperLimit:
     
        number = input("Enter number:")

        try:
        
            number = float(number) 
     
            if number.is_integer():
              
                integerList.append(number)
                numberCount += 1

                if numberCount == 1:
                    greatestNumber = number
                else:
                    if greatestNumber > integerList[-1]:
                        greatestNumber = greatestNumber
                    else:
                        greatestNumber = integerList[-1]
                    
                if greatestNumber in integerList:
                    index = integerList.index(greatestNumber)
            
        except:
            
            print("You entered a non-Integer, instead enter a +ve/-ve Integer!") 


    print("\n\nYou entered {} integers. The Largest number is: ".format(upperLimit) + str(int(greatestNumber)) + ", and its index is " + str(index) + "!")

else: 
    print("The value you entered is a non-Integer OR -ve Integer!")

 

Python program to specify Number of integers and Compute the Largest
Python | thetqweb