Python @ DjangoSpin

PyPro #17 Sum a List of Numbers with Exception Handling

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Sum a List of Numbers with Exception Handling in Python: A simple Python program of adding 3 given numbers, handling exception in case user inputs anything other than digits.

Sum a List of Numbers with Exception Handling in Python

# Sum of 3 numbers
# A simple program of adding 3 given numbers, handling exception in case user inputs anything other than digits.

# asking for inputs
try:
    numberOne = float(input("Please enter the first number: "))
    numberTwo = float(input("Please enter the second number: "))
    numberThree = float(input("Please enter the third number: "))
except ValueError:
    print("That was not a number!")

# displaying total
try:
    print("Total: ", str(numberOne + numberTwo + numberThree))
except NameError:
    print("Cannot compute sum. Please execute the program again.")
Try it here.

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply