course a201: introduction to programming

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

Upload: ward

Post on 23-Feb-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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” - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Course A201: Introduction to Programming

Course A201:Introduction to Programming

09/16/2010

Page 2: Course A201: Introduction to Programming

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”

Page 3: Course A201: Introduction to Programming

Boolean and Comparison Operators

• Boolean values: True or False

• Statements that will generate boolean values:1 == 1 True2 <= 0 False4 > 1 True9 != 99 True“1” == 1 False

These are Comparison operators

Page 4: Course A201: Introduction to Programming

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, fromThey will appear in orange in .py files in IDLE.

Page 5: Course A201: Introduction to Programming

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(“…”)

Page 6: Course A201: Introduction to Programming

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 …:

if …: some statements

Page 7: Course A201: Introduction to Programming

Conditional: WHILE

gpa = 0.0while 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

Page 8: Course A201: Introduction to Programming

Keep trace

• What’s wrong with this program:

health = 10trolls = 0damage = 3while health != 0:

trolls += 1health -= damage

Page 9: Course A201: Introduction to Programming

Keep trace

Health trolls damage health!= 010 0 3 True7 1 3 True 4 2 3 True 1 3 3 True -2 4 3 True -5 5 3 True -7 6 3 True

Page 10: Course A201: Introduction to Programming

Keep trace

• What’s wrong with this program:

health = 10trolls = 0damage = 3while health != 0:

trolls += 1health -= damage

<- This condition will be true FOREVER!!!

Page 11: Course A201: Introduction to Programming

Keep trace

• What’s wrong with this program:

health = 10trolls = 0damage = 3while health > 0:

trolls += 1health -= damage

<- You have to confirm that this condition will be false at some time

Page 12: Course A201: Introduction to Programming

Usage of break

gpa = 0.0while True:

gpa = float(raw_input(“Enter GPA: ” ))if gpa > 4.0:

break

• Break out of the loop

Page 13: Course A201: Introduction to Programming

Usage of continue

counter = 0while counter <= 10:

count += counterif count == 5

continueprint(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.

Page 14: Course A201: Introduction to Programming

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

Page 15: Course A201: Introduction to Programming

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

Page 16: Course A201: Introduction to Programming

Have a nice evening! See you tomorrow~