From a800a5f144815b058fcebf17919a8830211852c3 Mon Sep 17 00:00:00 2001 From: bachkukkik Date: Sat, 31 Oct 2020 19:26:27 +0700 Subject: [PATCH] add try-except cluase for sum.py --- Python/PythonBasics/sum.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Python/PythonBasics/sum.py b/Python/PythonBasics/sum.py index 8a27fca..16ddd51 100644 --- a/Python/PythonBasics/sum.py +++ b/Python/PythonBasics/sum.py @@ -1,12 +1,19 @@ a = int(input("Enter 1st Number")) b = int(input("Enter 2st Number")) -c = a + b -if c <= 20 : - print("no operation performed") -elif c > 20 and c <= 40 : - print("product is ",(a*b)) -elif c > 40 and c <= 60 : - print("difference is ",(a-b)) -else : - print("sum is ",c) \ No newline at end of file +try: + ## make sure if a, b are number + for e in [a, b]: + assert type(e) in [int, float] + c = a + b + + if c <= 20 : + print("no operation performed") + elif c > 20 and c <= 40 : + print("product is ",(a*b)) + elif c > 40 and c <= 60 : + print("difference is ",(a-b)) + else : + print("sum is ",c) +except: + print("invalid input") \ No newline at end of file