CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Computer Science 1620 Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
CPS120 Introduction to Computer Science Iteration (Looping)
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Course A201: Introduction to Programming 09/16/2010.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
CPSC 217 T03 Week V Part #1: Iteration Hubert (Sathaporn) Hu.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
9. ITERATIONS AND LOOP STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Topic: Recursion – Part 2
Topic: Functions – Part 1
Topic: Iterative Statements – Part 1 -> for loop
Topic: Conditional Statements – Part 1
Topic: Python’s building blocks -> Variables, Values, and Types
Repetition (While-Loop) version]
Topic: Conditional Statements – Part 2
Python: Control Structures
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Topic: Functions – Part 2
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
CiS 260: App Dev I Chapter 4: Control Structures II.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Topics Introduction to Repetition Structures
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Python Primer 2: Functions and Control Flow
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Alternate Version of STARTING OUT WITH C++ 4th Edition
IST256 : Applications Programming for Information Systems
Computing Fundamentals
Chapter 5: Control Structures II (Repetition)
Repetition Statements (Loops) - 2
COMPUTER PROGRAMMING SKILLS
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Another Example Problem
Based on slides created by Bjarne Stroustrup & Tony Gaddis
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
CMPT 120 Lecture 16 – Unit 3 – Graphics and Animation
Topic: Iterative Statements – Part 2 -> for loop
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
CMPT 120 Lecture 15 – Unit 3 – Graphics and Animation
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
Iteration – While Loops
Lecture 17 – Practice Exercises 3
Lecture 20 – Practice Exercises 4
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Lecture 20 – Practice Exercises 4
Lecture 17 – Practice Exercise 3 SOLUTIONS
Presentation transcript:

CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption – The realm of secret codes Python – Functions and While loop

Functions

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

Last lecture, we were about to … https://repl.it/repls/TimelyJovialAssociate

Execution Flow with Functions Let’s introduce a new tool: Python code visualizer

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)

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:

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

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

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

While loop

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’

Back to our Guessing Game Can we fix our Guessing Game?

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>

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

Review - Difference between while and for loops

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

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

GPS: Not to be used in this course! while True : while 1 : break continue pass Why?

Next Lecture We are having our Midterm 1 in B9201 at 9:30am See you there!