File tree 13 files changed +156
-0
lines changed
13 files changed +156
-0
lines changed Original file line number Diff line number Diff line change
1
+ try :
2
+ x = int (input ("Enter x: " ))
3
+ print (f"You entered { x } " )
4
+ except ValueError :
5
+ print ("Oops x isn't an Integer" )
Original file line number Diff line number Diff line change
1
+
2
+ class HttpException (Exception ):
3
+ statusCode = None
4
+ message = None
5
+ def __init__ (self ):
6
+ super ().__init__ (f"Status Code: { self .statusCode } ! Message: { self .message } " )
7
+
8
+ class NotFoundError (HttpException ):
9
+ statusCode = 404
10
+ message = "Server Not Found"
11
+
12
+ class ServerError (HttpException ):
13
+ statusCode = 500
14
+ message = "Server is down! We shall soon be vailable"
15
+
16
+ def raiseNotFoundError ():
17
+ raise NotFoundError ()
18
+
19
+ raiseNotFoundError ()
Original file line number Diff line number Diff line change
1
+ try :
2
+ x = int (input ("Enter x: " )) # x will not be created if it isn't any integer
3
+ except ValueError :
4
+ print ("Oops x isn't an Integer" )
5
+ else :
6
+ print (f"You entered { x } " )
Original file line number Diff line number Diff line change
1
+ while True :
2
+ try :
3
+ x = int (input ("Enter x: " )) # x will not be created if it isn't any integer
4
+ except ValueError :
5
+ print ("Oops x isn't an Integer" )
6
+ else :
7
+ break
8
+ print (f"You entered { x } " )
Original file line number Diff line number Diff line change
1
+ def main ():
2
+ z = get_int ()
3
+ print (f"You entered { z } " )
4
+
5
+ def get_int ():
6
+ while True :
7
+ try :
8
+ return int (input ("Enter x: " )) # x will not be created if it isn't an integer
9
+ except ValueError :
10
+ print ("Oops x isn't an Integer" )
11
+
12
+ main ()
Original file line number Diff line number Diff line change
1
+
2
+ # try ... except .... else ... finally
3
+
4
+ try :
5
+ nume = int (input ("Enter the numerator: " ))
6
+ denom = int (input ("Enter the denominator: " ))
7
+ quotient = nume / denom
8
+ print ("Correct division" )
9
+ except ZeroDivisionError :
10
+ print ("You can't divide by zero" )
11
+ except ValueError :
12
+ print ("Only integers required" )
13
+ else :
14
+ print ("Quotient =" , quotient )
15
+ finally :
16
+ print ("I am always here" )
Original file line number Diff line number Diff line change
1
+ num1 = int (input ("Num 1: " ))
2
+ num2 = int (input ("Num 2: " ))
3
+
4
+ if num2 == 0 :
5
+ raise ZeroDivisionError ("No division by zero" )
6
+ else :
7
+ print ("Quotient =" ,num1 / num2 )
Original file line number Diff line number Diff line change
1
+ ## Create your custom exceptions
2
+ # - clean code
3
+ # - documentation
4
+ # - set yourself away from others
5
+
6
+ class AgeErrorException (Exception ): # inheritance
7
+ def __init__ (self , value ):
8
+ self .value = value
9
+
10
+ try :
11
+ age = int (input ("What is your age?:" ))
12
+ if age < 0 or age >= 80 :
13
+ raise AgeErrorException ("Out of range age" )
14
+ print (f"You are { age } years OLD" )
15
+ except ValueError :
16
+ print ("We accept only integers" )
17
+ except AgeErrorException as ageErr :
18
+ print (ageErr )
Original file line number Diff line number Diff line change
1
+ ## Create your custom exceptions
2
+ # - clean code
3
+ # - documentation
4
+ # - set yourself away from others
5
+
6
+ class AgeErrorException (Exception ): # inheritance
7
+ def __init__ (self , value , message = "Age out of range" ):
8
+ self .value = value
9
+ self .message = message
10
+ super ().__init__ (self .message )
11
+
12
+ age = int (input ("What is your age? " ))
13
+ if not 0 < age < 80 :
14
+ raise AgeErrorException (age )
Original file line number Diff line number Diff line change
1
+ class HallAllocationError (Exception ):
2
+ pass
3
+
4
+ class HomeDistanceError (Exception ):
5
+ pass
6
+
7
+ def checkHomeDist (distance ):
8
+ maxDistance = 80
9
+ if distance < maxDistance :
10
+ raise HomeDistanceError ("You can commute from home" )
11
+ else :
12
+ print ("You may be allocated a hall" )
13
+
14
+ def checkAge (age ):
15
+ ageLimit = 30
16
+ if age > ageLimit :
17
+ raise HallAllocationError ("You are too old to be allocated a hall" )
18
+ else :
19
+ dist = int (input ("Distance = " ))
20
+ checkHomeDist (dist )
21
+
22
+ try :
23
+ age = int (input ("What is your age?" ))
24
+ checkAge (age )
25
+ except HallAllocationError as haErr :
26
+ print (haErr )
27
+ except HomeDistanceError as hoErr :
28
+ print (hoErr )
Original file line number Diff line number Diff line change
1
+ import sys
2
+
3
+ try :
4
+ print ("Hello there! I am" , sys .argv [1 ])
5
+ except IndexError :
6
+ print ("No arguments supplied" )
Original file line number Diff line number Diff line change
1
+ import sys
2
+
3
+ if len (sys .argv ) < 3 :
4
+ sys .exit ("Not enough args" )
5
+
6
+ elif len (sys .argv ) > 3 :
7
+ print ("A lot of args" )
8
+
9
+ else :
10
+ print ("Hello there! I am" , sys .argv [1 ])
Original file line number Diff line number Diff line change
1
+ import sys
2
+
3
+ if len (sys .argv ) < 3 :
4
+ sys .exit ("Not enough args" )
5
+
6
+ for x in sys .argv [1 :]:
7
+ print ("Hello there!" , x )
You can’t perform that action at this time.
0 commit comments