1 / 28

ECS 10

ECS 10. 10/8. Outline. Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin flipping (if time permits). Announcements. Professor Amenta will be back on Friday

gali
Download Presentation

ECS 10

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. ECS 10 10/8

  2. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin flipping (if time permits)

  3. Announcements Professor Amenta will be back on Friday Dan Alcantara will be covering Monday and Wednesday lectures E-mail address: dfalcantara@gmail.com Slides will be on class website Temporary location: http://idav.ucdavis.edu/~dfalcant/ecs10/10-08-07.ppt

  4. Homework 2 Homework 2 due Wednesday at 10PM Don’t use sys.exit(…) Will explain why later today Questions?

  5. Boolean expressions Boolean expression ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!”

  6. Boolean algebra • Named after George Boole (1815-1864) • Main idea: you can write down logic as mathematical formulas, as well as in sentences. • Logic as a computational system. Python does some of this computation!

  7. Boolean expressions Boolean is a new data type. Booleans can be either True or False. We now have four data types (and four kinds of expressions):

  8. Basic Boolean expressions Boolean expressions are True if the expression in English is True, and False otherwise.

  9. Boolean expression examples

  10. Compound Boolean expressions • Can combine multiple Boolean expressions with ‘and’ and ‘or’ keywords.

  11. The if statement dissected • ‘If’ statements change the flow of the program • Allows different things to happen under different conditions

  12. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” else: print “Welcome,”, name, “!” If the player doesn’t have a name, then: Call the player “Anonymous” Tell the player that he/she will be called “Anonymous” If the player does have a name, then: Welcome the player

  13. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” else: print “Welcome,”, name, “!” Check the player’s name Player didn’t give a name Player gave a name Rename the player “Anonymous” Welcome the player Tell the player they’ve been renamed Continue the program

  14. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” Check the player’s name Player didn’t give a name Player gave a name Rename the player “Anonymous” Tell the player they’ve been renamed Continue the program

  15. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next.

  16. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next. This is a block, which consists of a sequential set of lines indented the same way. The entire block is run when the condition is True.

  17. The if statement dissected ifname == “” : name =“Anonymous” print “We’ll call you”, name, “!” ‘if’ is a Python command that checks whether a condition is True or False and changes what the program does next. This is a block, which consists of a sequential set of lines indented the same way. The entire block is run when the condition is True. This is a condition, which is either True or False. This specific condition checks to see whether or not the variable “name” equals the empty string.

  18. Side note: Why sys.exit(…) is bad Some of you have been using it for program 2 Breaks the flow of the program Anything that is expected to happen after the sys.exit(…) call doesn’t happen

  19. Why sys.exit(…) is bad playerAnswer = raw_input(“What’s your answer? ”) if playerAnswer ==“42”: print “You’re right!” playerScore = playerScore + 1000 else: print “You got it wrong!” print“Your final score is”,playerScore Ask for the player’s answer Check the player’s answer Answered “42” Didn’t answer “42” Congratulate the player Insult the player Award the player 1000 points Print the final score

  20. Why sys.exit(…) is bad playerAnswer = raw_input(“What’s your answer? ”) if playerAnswer ==“42”: print “You’re right!” playerScore = playerScore + 1000 else: sys.exit(“You got it wrong!”) print“Your final score is”,playerScore Ask for the player’s answer Check the player’s answer Answered “42” Didn’t answer “42” Congratulate the player Insult the player Award the player 1000 points X Print the final score

  21. Booleans as state variables • State variables store information about the program’s current state • For homework 2, can be combined with ‘if’ statements to determine if program should stop • Their use avoids the need for sys.exit(…) call

  22. Example program: Coin flipping • Objective: • Python program will simulate flipping a coin • Coin will land on either heads or tails • User will guess either heads or tails before the flip • Problem: • Python doesn’t have any way of randomly flipping a coin • Need to add a module

  23. Our first module • A module is a collection of Python functions, maybe other stuff thrown in (library in other computer languages) • import the module to use its functions • For random numbers, need the random module • All the functions in module random are named random.something() ‘random’ module: Generates random numbers import random Your program

  24. Random module • Program that picks a number between 1 and 10 # get access to random number module import random # use function that picks a random # integer between 1 and 10 (inclusive) x = random.randint(1,10)

  25. Example program: Coin flipping • Use random.randint(1,2) to pick either 1 or 2 • If 1, then coin is heads. • If 2, then coin is tails.

  26. Start simple, then… • Now let user pick Heads or Tails • User’s choice has to match coin. • Uses Boolean and and or.

  27. Key command if (choice == "H" and coin == 1)\ or (choice == "T" and coin == 2): \ means statement continues on next line Two ways to win; if either situation occurs, user wins. Each situation is an and “or” them together

  28. Use to check input if (choice == "H") or (choice == "T"): if (choice != "H") and (choice != "T"): Or: And:

More Related