f u n c t i o n s r e t u r n v a l u e s
play

F u n c t i o n s R e t u r n V a l u e s - PowerPoint PPT Presentation

F u n c t i o n s R e t u r n V a l u e s Returns None Returns a value Output not reusable Output reusable 1 def fxn_avg (a, b): 1 def fxn_avg (a, b): 2 sum = a + b 2 sum = a + b 3 avg = sum/2 3


  1. F u n c t i o n s – R e t u r n V a l u e s  Returns None  Returns a value  Output not reusable  Output reusable 1 def fxn_avg (a, b): 1 def fxn_avg (a, b): 2 sum = a + b 2 sum = a + b 3 avg = sum/2 3 avg = sum/2 4 print (avg) 4 return avg 5 5 6 y = fxn_add (10, 10) 6 y = fxn_add (10, 10) 7 print (y) # None 7 print (y) # 10 8 type(y) 8 type(y) 9 # <class 'float'> 9 # <class 'NoneType'>

  2. F u n c t i o n s – R e t u r n V a l u e s  Only ONE value returned  Comma seperated values implicitly converted into tuple 1 def fxn_avg (a, b): 2 sum = a + b 3 avg = sum/2 4 return sum, avg 5 6 y = fxn_add (10, 10) 7 print (y) # (20, 10.0) 8 type(y) # <class 'tuple'>

  3. F u n c t i o n s – D u p l i c a t i o n  Adding arbitrary  Average of arbitrary numbers numbers 1 def fxn_add (*args): 1 def fxn_avg (*args): 2 sum = 0 2 sum = 0 3 for i in args: 3 count = len(args) 4 sum += i 4 for i in args: 5 return sum 5 sum += i 6 6 avg = sum/count 7 fxn_add (1,2,3) 7 return avg 8 7 fxn_avg (1, 2, 3)

  4. F u n c t i o n s – R e u s e & D e d u p l i c a t i o n 1 def fxn_add (*args): 1 def fxn_avg (*args): 2 sum = 0 2 sum = fxn_add(*args) 3 for i in args: 3 count = len(args) 4 sum += i 4 avg = sum/count 5 return sum 5 return avg 6 6 7 fxn_add (...) 7 fxn_add () 8 8

  5. F u n c t i o n s – R e u s e & D e d u p l i c a t i o n  Without reuse  With reuse 1 def fxn_avg (*args): 1 def fxn_avg (*args): 2 sum = 0 2 sum = fxn_add(*args) 3 count = len(args) 3 count = len(args) 4 for i in args: 4 avg = sum/count 5 sum += i 5 return avg 6 avg = sum/count 6 7 return sum 7 fxn_add () 8 9 fxn_avg (...)

  6. F u n c t i o n s – M o r e o n R e u s e  Module files  Module reuse # csc1017f.py 1 import csc1017f 2 3 csc1017f.fxn_1() 1 def fxn_1 (): 4 csc1017f.fxn_2() 3 return “fxn_1” 5 4 5 from csc1017f import fxn_3 5 def fxn_2 (): 6 6 return “fxn_2” 7 fxn_2() 7 6 8 def fxn_3 (): 7 fxn_add () 9 return “fxn_3”

  7. S T O P N – E x a m p l e s w i t h B u i l t i n s  Would this work? What is the result? What is the return type?  y = print(“hello”)  y = len(“four”)  y = input(“Enter value:”)

  8. UCT Department of Computer Science Computer Science 1017F Testing Lighton Phiri <lphiri@cs.uct.ac.za> April 2015

  9. I n t r o d u c t i o n  Wh a t i s a n e r r o r ?  Wh e n y o u r p r o g r a m d o e s n o t b e h a v e a s i n t e n d e d o r e x p e c t e d .  Wh a t i s a b u g ?  “ … a b u g c r e p t i n t o m y p r o g r a m … ”  D e b u g g i n g  t h e a r t o f r e m o v i n g b u g s

  10. T y p e s o f E r r o r s – S y n t a x E r r o r s  S y n t a x E r r o r s  F a i l u r e t o c o n f o r m t o P y t h o n i c s y n t a c t i c r u l e s  I m p r o p e r u s e o f P y t h o n l a n g u a g e – u s u a l l y S y n t a x E r r o r s .  1 2 v a r = 2 File "<stdin>", line 1 12var = 2 ^ SyntaxError: invalid syntax  F i x i n g S y n t a x E r r o r s  E a s i e s t t y p e o f e r r o r t o fi x  C o n f o r m ! F o l l o w t h e r u l e s !

  11. T y p e s o f E r r o r s – R u n t i m e E r r o r s  R u n t i m e E r r o r s  P r o g r a m i s s y n t a c t i c a l l y c o r r e c t b u t e r r o r d e t e c t e d a f t e r e x e c u t i o n .  a = 1/0 (ZeroDivisionError: division by zero)  a = b + 10  3 “four”[100] (IndexError: string index out of range)  I/O operations  F i x i n g R u n t i m e E r r o r s  E x c e p t i o n h a n d l i n g .  N o r m a l l y a b l e t o a n t i c i p a t e p o t e n t i a l p r o g r a m fl a w s  E x a m p l e s a b o v e i l l u s t r a t e t h i s

  12. T y p e s o f E r r o r s – R u n t i m e E r r o r s  E x c e p t i o n h a n d l i n g i n v o l v e d 1 def fxn_divide (a, b): 2 result = 0 3 try: 4 result = a / b 5 except ZeroDivisionError: 6 print (“Division by ZERO error!”) 7 return result 8 9 fxn_divide(1, 0)

  13. T y p e s o f E r r o r s – L o g i c a l E r r o r s  L o g i c a l E r r o r s  P r o g r a m p a r s e d b y b y i n t e r p r e t e r a n d r u n s s u c c e s s f u l l y , b u t p r o d u c e s u n d e s i r a b l e r e s u l t s  sum = 1 - 1  Conditionals—logical operators—and, or, not  F i x i n g L o g i c a l E r r o r s  P r o g r a m m e r r e s p o n s i b i l i t y t o e n s u r e c o d e f u n c t i o n s a s r e q u i r e d b e f o r e r e l e a s i n g i t  D e b u g g i n g  T e s t i n g

  14. S T O P 1 : I d e n t i f y i n g E r r o r s 1 def add_x (a, b): 2 return a + b 3 print (“Output”) # Logical Error 4 5 add_x(“1”, “2”) # Logical Error 6 add_x(+) # Syntax Error 7 add_x() # Runtime Error  I d e n t i f y e r r o r s i n c o d e s n i p p e t —w h a t t y p e s a r e t h e y ?  A n i n d i v i d u a l r e c e n t l y w o n a $ 5 k G o o g l e b o u n t y f o r fi g u r i n g o u t t h a t Y o u T u b e v i d e o s c o u l d e a s i l y b e d e l e t e d . Wh a t e r r o r w a s i d e n t i fi e d ?

  15. D e b u g g i n g  Debugging errors bugs i s t h e p r o c e s s o f fi n d i n g o r i n t h e c o d e .  D e b u g g i n g t e c h n i q u e s  A p p l i c a t i o n s t a c k t r a c e s —A n d r o i d a p p , We b b r o w s e r s , O S e s  A debugger i s a t o o l f o r e x e c u t i n g a n a p p l i c a t i o n w h e r e t h e p r o g r a m m e r c a n c a r e f u l l y c o n t r o l e x e c u t i o n a n d i n s p e c t d a t a .  F e a t u r e s i n c l u d e :  s t e p t h r o u g h c o d e o n e i n s t r u c t i o n a t a t i m e  v i e w i n g v a r i a b l e s  i n s e r t a n d r e m o v e b r e a k p o i n t s t o p a u s e e x e c u t i o n

  16. B a s i c D e b u g g i n g T e c h n i q u e s  C o m m e n t i n g o u t c o d e t h o u g h t t o b e r o o t c a u s e o f p r o b l e m 1 # result = a/b # possible division by 0 2 if b > 0 3 result = a/b 4  B u i l t i n o u t p u t f u n c t i o n  p r i n t ( … )

  17. D e b u g g i n g w i t h Wi n g  H a n d s o n L a b o r a t o r y E x e r c i s e  N e x t w e e k  S e t b r e a k p o i n t s i n t h e c o d e t o h a l t e x e c u t i o n p a t h  I n i t i a t e e x e c u t i o n i n d e b u g m o d e , s t a r t d e b u g g i n g  F a c i l i t a t e u s e r i n p u t / o u t p u t i n I / O D e b u g w i n d o w  S t e p t h r o u g h c o d e a n d v i e w —i n r e a l - t i m e —v a r i a b l e c h a n g e i n s t a c k d a t a w i n d o w  S t o p d e b u g g i n g w h e n e r r o r i s l o c a t e d

Recommend


More recommend