Download Exercise on control flow

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

C syntax wikipedia , lookup

Video game programmer wikipedia , lookup

Indentation style wikipedia , lookup

Control table wikipedia , lookup

C Sharp syntax wikipedia , lookup

One-pass compiler wikipedia , lookup

Structured programming wikipedia , lookup

Transcript
Making Choices (Selection) The ability to select what actions to perform based on circumstances we encounter is critical in real life and also in programming. A choice of several actions is expressed using an if-­‐statement. This exercise focuses on using if-­‐statements to control the flow of execution. Part (a): With a partner, play the game “What kind of animal were you in a prior lifetime?” After you have played it a couple of times, we will demo a python program that automates the game. Part (b): With a partner, download “game1.py”. Execute it a few times. Study it and be ready to answer the following questions about it. 1. What do statements #0-­‐#4 do? 2. What do statements #5 and #7 do? 3. What lines of code make up the if-­‐statement that starts at #6? (How does Python tell where the if-­‐statement stops?) 4. What lines of code make up the if-­‐statement that starts at #8? (How does Python tell where this if-­‐statement stops?) 5. What lines of code are executed if the user types a. “Y” at line #5 and “n” at line #7? b. “y” at line #5 and “y” at line #7? c. “n” at line #5 and “Y” at line #7? d. “yes” at line #5? (Is this what the programmer wanted?) 6. What parts of the flow chart from part (a) are implemented by this program? Part (c): Finish implementing this program incrementally, as follows: 1. Add an if-­‐statement that implements the True branch of the next question, but just prints “NOT DONE” on the False branch. 2. Add an if-­‐statement that implements both branches of the last question on the flow chart. Part (d): You and a partner will figure out the flow diagram that we used for a second game by repeatedly “testing it”. A peer leader will run the program repeatedly while students tell them what to respond at each prompt until all groups have worked out the flow diagram. One group will be called up to show their flow diagram to the class. Part (e): If there is time remaining, add error checking to your program in part (c). In other words, modify the program so that it plays the game as shown in the diagram provided that the user enters ‘Y’or ‘y’ or ‘N’ or ‘N’ at each prompt; but prints an error message and quits, if the user types any other input at any of the prompts.