Sunday, July 7, 2019

Sum of two numbers --- the simple method

Script

#!/usr/local/bin/python3.4

import sys

sum = 0


message = 'Enter a number:  '

try:
  value1 = float(input(message))
except ValueError:
  print("Not a float")
  sys.exit()
except Exception:
  print("Enter a valid number")
  sys.exit()

message = 'Enter a second number:  '

try:
  value2 = float(input(message))
except ValueError:
  print("Not a float")
  sys.exit()
except Exception:
  print("Enter a valid number")
  sys.exit()

sum =  value1 + value2


print('The sum of ', value1, 'and', value2, 'is', sum)



Execution


Enter a number:  36

Enter a second number:  91
The sum of  36.0 and 91.0 is 127.0



Enter a number:  173.67

Enter a second number:  872.49
The sum of  173.67 and 872.49 is 1046.16



Enter a number:  garbage

Not a float


Enter a number:  funny

Not a float

No comments:

Post a Comment