FIT1008 Lecture Notes - Lecture 4: Defensive Programming, Precondition, Unit Testing

84 views3 pages
!"#$%#&'()*&#+'#&"%*,
-.$%)')/0$1"%2&#&"%)&*%3#)4$#)562$7$%*&8$)/0"90'44&%96:
;
-.$%)0$'2&%9)70"4)&%/+#)5&<$<)7&($=)*10$$%=)$#1:
;
Exception-handling >)4$1.'%&*4)#")#0'%*7$0)1"%#0"()#")').'%2($0
?@1$/#&"% >)0+%#&4$)$8$%#)#.'#)A0$'B*)#.$)%"04'()7("C)"7)$@$1+#&"%=)'($0#&%9)1"2$)
*")&#)B%"C*)*"4$#.&%9)C$%#)C0"%9
?@1$/#&"%).'%2($0 >)A("1B)"7)1"2$)#.'#)1'%)0$1"8$0)70"4)#.$)$8$%#
?@'4/($,)A("1B*)"7)1"2$)#.'#)1'(()$'1.)"#.$0)5D)1'((*)E=)C.&1.)1'((*)F=)C.&1.)1'((*)GH:
I
J
?
G
F
E
D
M7)F)1'#1.$*)#.$)$00"0=)&#)C&(()#0L)#")0$*"(8$)#.$)&**+$<
M7)F)0$*"(8$*)$00"0=)1"2$)1"%#&%+$*)70"4)"0&9&%'()A("1B)#.'#)#.0$C)$@1$/#&"%<
M7)F)1'%3#)0$*"(8$=)&#)&#*$(7)0'&*$*)$@1$/#&"%)7"0)A("1B*)A$("C)5D)'%2)E:)#")1'#1.<
M7)D)"0)E)1'%3#)1'#1.)&#=)$@$1+#&"%)&*)'A"0#$2<
5&%)!L#."%:
try:
?@$1+#$)#0L)1('+*$
x = int(input("Enter number: "))
M7)%")$@1$/#&"%)&%*&2$)#0L=
print("Thanks!")
NB&/)$@1$/#)1('+*$)'%2)1"%#&%+$
except ValueError:
M7)#.$0$)&*)$@1$/#&"%
print("Not a valid number.")
NB&/)#.$)0$*#)"7)#0L)1('+*$
M7)$00"0)#L/$)4'#1.$*,
?@$1+#$)$@1$/#)1('+*$
?@$1+#&"%)1"%#&%+$*)'7#$0)#0L)*#'#$4$%#
M7)$00"0)#L/$)2"$*%3#)4'#1.,
O$@#)$@1$/#)1('+*$)&*)1.$1B$2
M7)%"%$)0$4'&%=)P+4/)#")"+#$0)#0L)*#'#$4$%#*
M7)%").'%2($0)7"+%2=)$@1$/#&"%)&*)+%.'%2($2=)*")$@$1+#&"%)*#"/*)C&#.)')
4$**'9$
!"**&A($)#")0'&*$)"C%)$@1$/#&"%* #"",
def get_height():
h = int(input("Please enter your height (in cm): "))
if h <= 0:
my_error = ValueError("Height should be greater than
zero!")
raise my_error
return h
raise 9$#*)+*)"+#)"7)%"04'()$@$1+#&"%<)F'(($0) #'B$*)1"%#0"(H)+%#&()&#)7&%2*)').'%2($0)
7"0)Q'(+$?00"0<
RL/$ S'&*$2)C.$%H
T$LA"'02M%#$00+/# F%#0(UF)&*).&#
V8$07("C?00"0 J("'#&%9)/"&%#)#"").&9.
W$0"G&8&*&"%?00"0 G&8&2$)AL)X$0"
MV?00"0 -.$%)MKV)"/)7'&(*
M%2$@?00"0 -.$%)&%2$@)"+#*&2$)8'(+$)0'%9$
O'4$?00"0 D##$4/#)#")$8'(+'#$)'%)+%'**&9%$2)8'0&'A($
RL/$?00"0 D%)"/)&*)'//(&$2)#")'%)"AP)"7)C0"%9)#L/$
Q'(+$?00"0 -.$%)"/K7+%1#&"%).'*)'%)'09)CK)&%1"00$1#)8'(+$
Assert3statement
>)+*$7+()7"0)1.$1B&%9)/0$1"%2&#&"%*
!0$1"%2&#&"%)>)1"%2&#&"%K/0$2&1'#$)#.'#)4+*#)'(C'L*)A$)#0+$)P+*#)/0&"0)#")
$@$1+#&"%)"7)')*$1#&"%)"7)1"2$
M7)&#)2"$*%3#)."(2=)$77$1#)"7)1"2$)&*)+%2$7&%$2=)'%2)4'L)%"#)1'00L)"+#)
&%#$%2$2)C"0B
;
def power(x,n):
assert n > 0, "n should be a positive integer"
value = 1
if n > 0:
value = power(x, n//2)
if n % 2 == 0:
value = value * value
else:
value -value * value * x
return value
Unit3testing
>)#$*#)$'1.)+%&#)"7)1"2$)*$/'0'#$(L)5#L/&1'((L)Y)+%&#)>)Y)7+%1#&"%:
!0"*,
M%10$'*$*)1"%7&2$%1$)#.'#)1"2$)C&(()C"0B)'*)$@/$1#$2;
Z'B$*)0$7'1#"0&%9)$'*&$0;
F'%)/$04'%$%#(L)7&@)A+9*;
J"0)$'1.)4"2+($=)10$'#$)')*$/'0'#$)7&($<
!0"1$2+0$,
N$#)+/)#$*#&%9)1"%2&#&"%*Y<
F'(( 4$#."2[<
Q$0&7L)"+#/+#)+*&%9)'**$0#\<
F($'%)+/)&7)%$1$**'0L]<
Week$4:$error$handling
R.+0*2'L=)Y])^+%$)[_Y`
Y],YY
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents