cosc 1306—computer science and programming python branches and loops jehan-françois pâris...

20
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris [email protected]

Upload: elmer-walton

Post on 14-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING

PYTHON BRANCHES AND LOOPS

Jehan-François Pâ[email protected]

Page 2: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

STARTING WITH A GAME

Page 3: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Number guessing game

• We use a random number generator to generate a random number between 1 and 10

• Users are asked to guess the number until they come with the right value

Page 4: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Number guessing game

• We use a random number generator to generate a random number between 1 and 10

• Users are asked to guess the number until they come with the right value

Page 5: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Flow chartGenerate RN n

You win

Input i

i == nTrueFalse

Page 6: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

More about flow charts

• Graphic representation of flow of control– Loops represented by loops– Ifs have two branches

???

False True

Page 7: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Simple if

• If condition :something

• If tank_empty :get_gas

condition

False

True

something

Page 8: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

False True

If with else clause (I)

• If condition:something

else:other stuff

other stuff something

condition

Page 9: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

If with else clause (II)

• If hungry :Macdonald

else :Starbucks

• In either case, you will leave the freeway

Page 10: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

While

something

True

False

• while condition :something

• while stain :

keep_washing

condition

Go back!

Page 11: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

How to generate RN

• Import a function from module random– from random import randint

• randint(min, max)– generates an integer between min and max

Page 12: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

The program (I)

• # guess.py“”” must guess a random number between 1 and 10“””# secret# guess

Page 13: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

The program (II)

• from random import randintsecret = randint (1,10)guess = -1 # bad guesswhile guess != secret :

guess = int(input("Enter your guess: ")print("You win!")

Page 14: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

A better program

• Tells whether guess is too low or too high

• Must consider three cases inside the loop– Correct guess– Guess that is too low– Guess that is too high

Page 15: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

The better program (I)

• # guess.py“”” must guess a random number between 1 and 10“””# secret# guessfrom random import randintsecret = randint (1,10)guess = -1 # bad guess

Page 16: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

The program (II)

• while guess != secret :guess = int(input(“Enter your guess: “))if guess == secret :

print ("You win!")else :

if guess < secret : print ("Too low!")

else: print ("Too high!")

Page 17: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Using an elif (I)

• Too many nested ifs

if cond-1 : …

else if cond-2 : …

else if cond-3 : …

else …

Page 18: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Example

while guess != secret : guess = int(input("Enter your guess: ")) if guess == secret : print ("You win!") elif guess < secret : # observe indentation print ("Too low!") else: print ("Too high!")

Page 19: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Using an elif (II)

• With elif, lines align better

if cond-1 : …

elif cond-2 : …elif cond-3 : …else

Page 20: COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS Jehan-François Pâris jfparis@uh.edu

Indenting advice

• Python attaches great importance to indenting– You cannot indent anything that is not inside

• An if, a while, a for, …• A function declaration or a main function

– Your indentation must be consistent• Do not mix spaces and tabs