Course A201: Introduction to Programming 09/16/2010.

Slides:



Advertisements
Similar presentations
CMPT 100 : INTRODUCTION TO COMPUTING TUTORIAL #5 : JAVASCRIPT 2 GUESSING GAME By Wendy Sharpe 1.
Advertisements

ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Conditional Statements Introduction to Computing Science and Programming I.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
INTRODUCTION TO PYTHON PART 3 - LOOPS AND CONDITIONAL LOGIC CSC482 Introduction to Text Analytics Thomas Tiahrt, MA, PhD.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Python Control of Flow.
Simple Python Loops Sec 9-7 Web Design.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
An Introduction to Textual Programming
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
COMP 1001: Introduction to Computers for Arts and Social Sciences Programming in Scratch Monday, May 16, 2011.
Control Structures FOR Statement Looping.
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
Branching and Conditions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
8. DECISION STRUCTURES Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
Control Flow (Python) Dr. José M. Reyes Álamo. 2 Control Flow Sequential statements Decision statements Repetition statements (loops)
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
Control Flow (Python) Dr. José M. Reyes Álamo.
Topic: Iterative Statements – Part 1 -> for loop
Week of 12/12/16 Test Review.
Introduction to Python
Guide to Programming with Python
Selection and Python Syntax
Python: Control Structures
ECS10 10/10
IF statements.
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.
Topics Introduction to Repetition Structures
Logical Operators and While Loops
Selection CIS 40 – Introduction to Programming in Python
And now for something completely different . . .
Learning to Program in Python
Introduction to Programming
Conditional and iterative statements
Computer Science Core Concepts
CISC101 Reminders All assignments are now posted.
Logical Operators and While Loops
Programming Concepts and Database
CHAPTER 5: Control Flow Tools (if statement)
Repetition Statements (Loops) - 2
Python While Loops.
Introduction to Programming
CMPT 120 Lecture 4 – Unit 1 – Chatbots
CMPT 120 Lecture 13 – Unit 2 – Cryptography and Encryption –
Iteration – While Loops
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Course A201: Introduction to Programming 09/16/2010

Outlines for today A new type of value: Boolean Concept of keyword and indentation Several basic concepts about Branching – IF - ELSE – IF and WHILE loop Keep trace: what’s the stop condition How and when to use “break” and “continue” Demonstration of “Guess My Number Game”

Boolean and Comparison Operators Boolean values: True or False Statements that will generate boolean values: 1 == 1True 2 <= 0False 4 > 1True 9 != 99True “1” == 1False These are Comparison operators

keyword In Assignment 1, question 2, the last variable is “for”, which seams legit but actually not This is a KEYWORD Keywords are a set of words that have special/predefined meanings: for, if, else, while, break, import, from They will appear in orange in.py files in IDLE.

Indentation In Python, proper indentations are crucial -> Python use indentations to determine the structure of if- else blocks, while blocks, etc. If you forget to have indentations, it will give you syntax error Enter tab if user == “bye”: print ( “bye!” ) elif user == “hi”: print( “Hi” ) else: print(“…”)

Conditional: IF if user == “bye”: print ( “bye!” ) elif user == “hi”: print( “Hi” ) else: print(“…”) Don’t forget the COLON You can have only IF, or IF- ELSE, or IF-ELIF, or IF-ELIF- ELSE You can have another IF block inside a IF block: if …: some statements

Conditional: WHILE gpa = 0.0 while gpa <= 4.0: gpa = float(raw_input(“Enter GPA: ” )) print(“Not high enough” ) While [this condition] is true, keep executing the [statements in here ] Creating loops, make repetitive tasks easier

Keep trace What’s wrong with this program: health = 10 trolls = 0 damage = 3 while health != 0: trolls += 1 health -= damage

Keep trace Healthtrollsdamagehealth!= True 71 3 True 4 23 True True True True True

Keep trace What’s wrong with this program: health = 10 trolls = 0 damage = 3 while health != 0: trolls += 1 health -= damage <- This condition will be true FOREVER!!!

Keep trace What’s wrong with this program: health = 10 trolls = 0 damage = 3 while health > 0: trolls += 1 health -= damage <- You have to confirm that this condition will be false at some time

Usage of break gpa = 0.0 while True: gpa = float(raw_input(“Enter GPA: ” )) if gpa > 4.0: break Break out of the loop

Usage of continue counter = 0 while counter <= 10: count += counter if count == 5 continue print(counter) print(“-” * 20) The output won’t include 5. When you reach continue, the rest of code (in the red rectangle) will be omitted, computer goes straight to next interation.

Guess My Number Game pick a random number while the player hasn't guessed the number, let the player guess If guess is right, congratulate the player

Lab work Write a program that flips a coin 100 times and then tells you the number of head and tails Modify your program so that it prompts the user for the number of times to flip the coin On paper in English first, on computer in Python next Optional team work

Have a nice evening! See you tomorrow~