Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
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.
Fundamentals of Python: From First Programs Through Data Structures
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Fundamentals of Python: First Programs
Decision Structures and Boolean Logic
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Variables and Expressions CMSC 201 Chang (rev )
Computer Science 111 Fundamentals of Programming I Making Choices with if Statements.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 5, Lecture 1 (Monday)
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
The IF-statements 31-Jan-2005 Venkatesh Ramamoorthy.
Decision Structures, String Comparison, Nested Structures
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Control Structures I Chapter 3
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
GCSE COMPUTER SCIENCE Practical Programming using Python
CMPT 120 Topic: Python’s building blocks -> More Statements
Topic: Functions – Part 1
CprE 185: Intro to Problem Solving (using C)
Topic: Iterative Statements – Part 1 -> for loop
Introduction to Python
Topic: Python’s building blocks -> Statements
Topic: Conditional Statements – Part 1
Making Choices with if Statements
Sequence, Selection, Iteration The IF Statement
Topic: Conditional Statements – Part 2
Selection and Python Syntax
CMPT 201 if-else statement
Python: Control Structures
Decision Making in Code Logical Tests & Truth Logical Expressions
Topics The if Statement The if-else Statement Comparing Strings
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.
Expressions and Control Flow in JavaScript
Javascript Conditionals.
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 The if Statement The if-else Statement Comparing Strings
And now for something completely different . . .
Relational Operators Operator Meaning < Less than > Greater than
Types, Truth, and Expressions (Part 2)
Coding Concepts (Basics)
Logical Operations In Matlab.
Selection Control Structure
Selection Statements.
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Adding Intelligence Check for the presence or absence of a condition in the environment Take the appropriate actions Involves making a choice (to do or.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
Repetition Statements (Loops) - 2
CS 101 First Exam Review.
Topic: Iterative Statements – Part 2 -> for loop
CMPT 120 Lecture 10 – Unit 2 – Cryptography and Encryption –
CMPT 120 Lecture 6 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Fundamentals of Python: First Programs Second Edition
Programming Fundamental
Presentation transcript:

Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness CMPT 120 Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness

Homework - Show Case Problem Statement Write a login program, which allows a user to login in with a password Thank you for sending your link https://repl.it/repls/ForcefulTautGoals https://repl.it/repls/SingleGorgeousUpgrades https://repl.it/repls/SuperbDownrightChapter https://repl.it/repls/VillainousSandybrownState https://repl.it/@AnsonWong1/YearlyAbleModes

Review - Little Exercise version 1 + Answers if condition 1 : if condition 2 : some statements A if condition 3 : some statements B else : some statements C if condition 4 : some statements D some statements E some statements F some statements G 1. Which statements are executed if condition 1 is False? Answer: F and G 2. Which statements are executed if condition 1 is True and condition 2 is False? Answer: E and G

Review - Little Exercise version 2 + Answers if condition 1 : if condition 2 : some statements A if condition 3 : some statements B else : some statements C if condition 4 : some statements D some statements E some statements F some statements G 1. To execute statements A B D G, to what Boolean value would conditions 1, 2, 3 and 4 need to evaluate? Answer: condition 1, 2, 3, and 4 would need to be True. 2. To execute statements A B C G, to what Boolean value would conditions 1, 2, 3 and 4 need to evaluate? Answer: It is impossible to have statements A B C G executed together.

Reading Review What does the string strip function (method) do? What string function (method) should we use to replace all occurrences of “8” in a string with “9”? What could be wrong with this code? if reply.lower().strip(" ") == "GOOD" print("Good!") What does this code output? movies = ["Superman", "Frozen", "X-Men"] print("x-men" in movies)

Review - Examples of Boolean "great" == "Great" -> False "Great!" == "Great" -> False "Great!" == "Great!" -> True "23" < "19" -> False  "100" < "19" -> True  How does this work? How can we compare characters?

ASCII Code (part of Unicode)

Reading Review What we can do with the "in" keyword for lists https://repl.it/repls/YawningSpanishOpposites

Let’s build a simple game! Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 My solution: https://repl.it/repls/QuaintBouncyHarddrive

Guessing Game Testing our guessing game: Test case 1 : input != number to guess Test case 2 : input == number to guess Test case 3 : invalid input -> 53 (outside range)

Your turn! Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 Requirements: Let’s make your guessing game more responsive How? By telling the user when she/he has entered a number that was < 1 “You have entered a number < 1!” or > 10 “You have entered a number > 10!”

Improving our Guessing Game Problem Statement Write a guessing game, which allows a user to guess a number between 1 and 10 Requirements: Let’s make your guessing game more robust So it does not crash when the player enters unexpected input like “banana” My Solution: https://repl.it/repls/IroncladUnhealthyDaemons

You have 3 guesses! Wouldn’t it be nice to play our guessing game many times without having to press Run over and over again? Problem Statement Write a guessing game, which allows a player to guess a number between 1 and 10 in 3 guesses! My Solution: Use a for loop (iteration) https://repl.it/repls/LeanCyanInstances

Review – Terminology related to functions We call a function by name Example of a function call: userName = input("Please, enter your name: ") The name of the function is input The expression in parentheses is called the argument of the function We say that a function takes an argument(s) The result of the function is what the function produces In the above example, the result of the function is what the user has typed, i.e., her/his name, which is assigned to userName ‘Anne’ We say that a function returns a result and the result is called the returned value

Review – type( ): another useful function Built-in function Syntax: type(<expression>) How it works: The expression is evaluated type(…) is called with the result of the expression as its argument type(…) returns a value which is the data type of its argument, i.e., the result of the expression Examples: type(123) type("Hello") type("123") pi = 3.14 type(pi)

How to construct a condition? SYNTAX: <operand> <operator> <operand> Relational operators (or comparison operators) < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to Variable Literal value -> both of type string, integer or float Variable Literal value -> both of type string, integer or float Result of conditional expression: True or False Relational operators (< , >=, ==, …) may connect expressions evaluating to different types of values

How to construct a condition? SYNTAX: .<method>(…) <function>(…) operator Example: <string>.isdigit( ) <string>.isalpha( ) etc… Example: all(…) etc… Example: in containment test operator Result of conditional expression: True or False We have built-in functions returning numerical values: len(“hello”) returns 5 int(“5”) returns 5 We have methods returning numerical values: “hello”.find(“lo”) returns 3 Similarly, we have built-in functions returning Boolean values: all([1<2,2<4,5==5]) returns True We have methods returning Boolean values: “123456”.isdigit() returns True “123456”.isalpha() returns False

What if we have more than 1 conditions? In our Guessing Game If the user enters a guess (a number) between 1 and 10 -> valid data If the user enters a guess < 1 or > 10 -> invalid data Guardian code (input validation code): if userGuess < 1 or userGuess > 10 : print("Your guess should be a number between 1 and 10...") # then terminate program else : ... This is called a compound condition: a condition that Is composed of > 1 condition.

What if we have more than 1 conditions? We could also create the following guardian code (input validation code): if userGuess >= 1 and userGuess <= 10 : # Did the user guess correctly? # Display appropriate message if number == userGuess : print("Wow! You got it!") else : print("Sorry! You missed! The number was %d." %number) ... compound condition

How to construct a logical expression (compound condition)? Conditions (Boolean expressions, i.e., expressions producing Boolean values), can be connected via the logical operators and, or, not creating logical expressions Such logical expressions are called compound conditions

How to construct a logical expression (compound condition)? SYNTAX for and & or: <operand> <logical_operator> <operand> not: not <operand> Logical operators operand and or operand Boolean expression Boolean expression not operand Result of compound conditional expression: True or False Boolean operators and, or must connect two Boolean expressions Boolean operator not must be applied to one Boolean expression

How to evaluate a logical expression (compound condition)? Boolean truth table: and truth table or truth table not truth table Source: http://www.slideshare.net/drjimanderson/ an-introduction-to-software-development-software-development-midterm-review-45791774

Next Lecture Let’s see how much we have learnt so far by having a little practice exam! We’ll have plenty of help while going through this activity