Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised...

25
Python: Structured Programming Damian Gordon

description

################ # Main Program # ################ # PROGRAM CheckPrime: if IsItPrime() == True: # THEN print("Prime number") else: print("Not a prime number") # ENDIF; # END.

Transcript of Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised...

Page 1: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Python: Structured Programming

Damian Gordon

Page 2: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

• Remember the modularised version of the prime number checking program:

Page 3: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

################# Main Program #################

# PROGRAM CheckPrime:

if IsItPrime() == True:# THEN print("Prime number")else: print("Not a prime number")# ENDIF;

# END.

Page 4: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

########################## Prime Checking Module ##########################

def IsItPrime(): a = int(input("Please input value: ")) b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime

# END IsItPrime.

Page 5: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

• In this version, the MAIN program the MAIN program calls the MODULE IsItPrime() which does the reading in of the value, and the checking to see if it is prime or not.

• Because good module design says each module should do one thing well, as opposed to two or three things kinda well, we should rewrite so that the MODULE called IsItPrime() just checks if a number is prime (and that number is passed into it).

Page 6: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

################# Main Program #################

# PROGRAM CheckPrime:a = int(input("Please input value: "))if IsItPrime(a) == True:# THEN print("Prime number")else: print("Not a prime number")# ENDIF;

# END.

Page 7: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

################# Main Program #################

# PROGRAM CheckPrime:a = int(input("Please input value: "))if IsItPrime(a) == True:# THEN print("Prime number")else: print("Not a prime number")# ENDIF;

# END.

Page 8: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

########################## Prime Checking Module ##########################

def IsItPrime(a): b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime# END IsItPrime.

Page 9: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

########################## Prime Checking Module ##########################

def IsItPrime(a): b = a - 1 IsPrime = True while b != 1: # DO if a % b == 0: # THEN IsPrime = False # ENDIF; b = b - 1 # ENDWHILE; return IsPrime# END IsItPrime.

Page 10: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Variable Scope

• The scope of a variable – is the part of a computer program where the binding is valid: where the variable name can be used to refer to the entity.

• In other parts of the program the variable name may refer to a different entity (it may have a different binding), or to nothing at all (it may be unbound).

• The scope of a binding is also known as the “visibility” of a variable.

Page 11: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"print(global_var)# END.

Page 12: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"print(global_var)# END.

This is a global variable

Page 13: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

Page 14: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): print(global_var)# END MyMethod

MyMethod()print(global_var)# END. This is a global variable

This is a global variable

Page 15: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): global_var = “Local copy of the global variable" print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

Page 16: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): global_var = “Local copy of the global variable" print(global_var)# END MyMethod

MyMethod()print(global_var)# END. This is a local copy of the global variable

This is a global variable

Page 17: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): global_var = “Local copy of the global variable" print(global_var) global global_var print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

Page 18: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = "This is a global variable"

def MyMethod(): global_var = “Local copy of the global variable" print(global_var) global global_var print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

This is a local copy of the global variableThis is a local copy of the global variableThis is a local copy of the global variable

Page 19: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = 4print(global_var)# END.

Page 20: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = 4print(global_var)# END.

4

Page 21: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = 4

def MyMethod(): print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

Page 22: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming

# PROGRAM Global and Local Variablesglobal_var = 4

def MyMethod(): print(global_var)# END MyMethod

MyMethod()print(global_var)# END. 4

4

Page 23: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = 4

def MyMethod(): global_var = 8 print(global_var)# END MyMethod

MyMethod()print(global_var)# END.

Page 24: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

Structured Programming# PROGRAM Global and Local Variablesglobal_var = 4

def MyMethod(): global_var = 8 print(global_var)# END MyMethod

MyMethod()print(global_var)# END. 8

4

Page 25: Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version…

etc.