Python program to specify Number of Integers and Compute their Sum
Python program to specify Number of Integers and Compute their Sum
Below is the Python program that given a list of integers, computes their SUM. 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!” 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
upperLimit = input("How many integers do you want to enter to calculate their Sum?: ")
if upperLimit.isdigit() == True:
upperLimit = int(upperLimit) Python program to specify Number of Integers and Compute their Sum
print("Please enter {} positive numbers to calculate their sum! \n\n".format(upperLimit))
while numberCount < upperLimit:
number = input("Enter number:")
if number.isdigit() == True:
number = int(number)
numberCount += 1
total += number
else:
print("The value you entered is a non-Integer!")
print("\n\nYou entered {} positive numbers whose Sum is: ".format(upperLimit) + str(total) + "!")
else:
print("The value you entered is a non-Integer!")
Python program to specify Number of integers and Compute their Sum
Python | thetqweb