1 / 20

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS. Jehan-François Pâris jfparis@uh.edu. STARTING WITH A GAME. Number guessing game. We use a random number generator to generate a random number between 1 and 10

jenny
Télécharger la présentation

COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON BRANCHES AND LOOPS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. STARTING WITH A GAME

  3. 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

  4. 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

  5. Flow chart Generate RN n Input i False True i == n You win

  6. True False ??? More about flow charts • Graphic representation of flow of control • Loops represented by loops • Ifs have two branches

  7. Simple if • If condition : something • If tank_empty : get_gas True condition False something

  8. If with else clause (I) • If condition: somethingelse: other stuff False True condition other stuff something

  9. If with else clause (II) If hungry : Macdonaldelse : Starbucks In either case, you will leave the freeway

  10. While • while condition : something • while stain : keep_washing True condition False something Go back!

  11. How to generate RN • Import a function from module random • from random import randint • randint(min, max) • generates an integer between min and max

  12. The program (I) • # guess.py“”” must guess a random number between 1 and 10“””# secret# guess

  13. 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!")

  14. 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

  15. 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

  16. 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!")

  17. Using an elif (I) • Too many nested ifs if cond-1 : …else if cond-2 : … else if cond-3 : … else …

  18. 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!")

  19. Using an elif (II) • With elif, lines align better if cond-1 : …elif cond-2 : …elif cond-3 : …else …

  20. 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

More Related