Download presentation
Presentation is loading. Please wait.
Published byYuliana Widjaja Modified over 5 years ago
1
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
The realm of secret codes Python – Functions and While loop
2
Functions
3
Last lecture we saw that …
Function Takes parameters or not Returns a value or not Python code fragment that does one specific task A function has a descriptive name
4
Last lecture, we were about to …
5
Execution Flow with Functions
Let’s introduce a new tool: Python code visualizer
6
Different kinds of functions
Activity: Fill this table using functions we have used so far! Function takes parameters Function does not take parameters Function returns a result (i.e., a returned value) Function does not return a result (i.e., a returned value)
7
Are there “functions” in the real world?
Mom says to Louise “Please, can you go make your bed?“ Specific task: Make bed Parameter(s): None required Returned value: None returned Mom says to Louise “Here’s $5, please, can you go buy a bag of apples?“ Specific task: Parameter(s): Returned value:
8
Are there “functions” in the real world?
Mom says to Louise “Please, can you find my cell phone?“ Specific task: Parameter(s): Returned value: Mom says to Louise “Please, can you put these bags on the kitchen counter?“ Specific task: Put bags on kitchen counter
9
Homework – Using Repl.It …
Let’s create a function that returns the largest number out of 3 numbers Requirements: cannot use the max( ) built-in function cannot use the word max to name your function Let’s create a function that returns the sum of the numbers in a list Let’s create a function that prints “*” 5 times in a row
10
Review - Function Syntax of function definition
def <functionName>( [parameter(s)] ) : < 1 or more statements > return [expression] def -> means “here is the definition of a function” <functionName> (Good Programming Style - GPS) Function name is descriptive -> it describes its purpose Function name syntax: same as for variable name syntax Function definition -> Header of a function -> Body of a function -> One return statement
11
While loop
12
Activity Add a while loop to our encryption/decryption program that allows the user to encrypt/decrypt messages until s/he enters ‘S’ or ‘s’
13
Back to our Guessing Game
Can we fix our Guessing Game?
14
Review - Syntax of a while loop
<stmt: initialize condition variable> while <Boolean condition> : <first statement to be repeated> <second statement to be repeated> ... <stmt: modify condition variable> <statement outside (after) the loop>
15
Review - Syntax of a while loop
<stmt: initialize condition variable> while <Boolean condition> : <first statement to be repeated> <second statement to be repeated> ... <stmt: modify condition variable> <statement outside (after) the loop> Important – About Indentation Statements inside the loop (i.e., statements executed at each iteration of the loop) are the statements indented with respect to the while keyword Statements outside the loop (before and after the loop) are the statements that are not indented with respect to the while keyword – these statements are considered to be at the same level of indentation as the while loop
16
Review - Difference between while and for loops
17
When best to use a while loop
If we know there is a condition that will occur in our program and its occurrence will dictate when we stop repeating a set of statements, then we use the iterative statement called while loop That condition is often called a sentinel or flag Examples: User termination User enters yes/no (flag) or some special value (sentinel) User selects ‘X’ to eXit from a menu (menu-driven program) Occurrence of an error Reading data from a file -> EOF
18
Let’s challenge ourselves!
Let’s convert the previous while loop using the most appropriate iterative statement? fruit = ["banana", "apple", "plum"] index = 0 while index < len(fruit): print(fruit[index]) index = index + 1
19
GPS: Not to be used in this course!
while True : while 1 : break continue pass Why?
20
Next Lecture We are having our Midterm 1 in B9201 at 9:30am
See you there!
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.