Скачать презентацию Introduction to Computing and Programming in Python A Скачать презентацию Introduction to Computing and Programming in Python A

2f92a40c66119709d9f0a0c200718b1d.ppt

  • Количество слайдов: 29

Introduction to Computing and Programming in Python: A Multimedia Approach 4 ed Chapter 10: Introduction to Computing and Programming in Python: A Multimedia Approach 4 ed Chapter 10: Building Bigger Programs

Chapter Objectives Chapter Objectives

How to Design Larger Programs Building something larger requires good software engineering. Top-down: Start How to Design Larger Programs Building something larger requires good software engineering. Top-down: Start from requirements, then identify the pieces to write, then write the pices. Bottom-up: Start building pieces you know, test them, combine them, and keep going until you have your program Debugging: Programming is “the art of debugging a blank sheet of paper. ” Testing: Because nothing complicated and man-made is flawless. Maintenance: By far, the most expensive part of any program.

Top-Down Design Start from a problem statement. What are you trying to do? Refine Top-Down Design Start from a problem statement. What are you trying to do? Refine the problem statement. Use hierarchical decomposition to define subparts. Refine until you know how to write the programs. Use procedural abstraction so that higher-level functions are written in terms of lower-level.

What's an Adventure Game? Text-based, interactive fiction. Dates back to 1970's: http: //en. wikipedia. What's an Adventure Game? Text-based, interactive fiction. Dates back to 1970's: http: //en. wikipedia. org/wiki/Colossal_Cave_Adventure See Zork at https: //youtu. be/1 q 9 Q 2 gwqw 7 U Play Zork at http: //textadventures. co. uk/games/view/5 zyoqrsugeo pel 3 ffhz_vq/zork There are new and updated tools for making interactive fiction like Inform, http: //www. informfiction. org

Example Top-Down Design: An Adventure Game Top-level function: 1. Tell the user how to Example Top-Down Design: An Adventure Game Top-level function: 1. Tell the user how to play the game. 2. Describe the room. 3. Get the player's command. 4. Figure out the next room. 5. Return to Step 2, until the user Quits.

Two new functions print. Now(): Takes a string as input, and prints it on Two new functions print. Now(): Takes a string as input, and prints it on the Command Area immediately. Print waits until the program is done. request. String(): Takes a prompt string as input, accepts a string from the user in a dialog window, then returns the user's input.

An important new loop How do we keep going, indefinitely, until the user says An important new loop How do we keep going, indefinitely, until the user says “quit”? A while loop repeats a block until a test becomes false.

Writing the top level function Working directly from our earlier outline. This function makes Writing the top level function Working directly from our earlier outline. This function makes sense, even without knowing the lower level functions. It is decoupled from the lower-level. def play. Game (): location = "Porch" show. Introduction () while not (location == "Exit") : show. Room(location) direction = request. String("Which direction? ") location = pick. Room(direction , location)

Writing the subfunctions def show. Introduction (): print. Now( Writing the subfunctions def show. Introduction (): print. Now("Welcome to the Adventure House!") print. Now("In each room , you will be told which directions you can go. ") print. Now("You can move north , south , east , or west by typing that direction. ") print. Now("Type help to replay this introduction. ") print. Now("Type quit or exit to end the program. ") def show. Room(room ): if room == "Porch": show. Porch () if room == "Entryway": show. Entryway () if room == "Kitchen": show. Kitchen () if room == "Living. Room": show. LR () if room == "Dining. Room": show. DR ()

pick. Room() def pick. Room(direction , room ): if (direction == pick. Room() def pick. Room(direction , room ): if (direction == "quit") or (direction == "exit"): print. Now("Goodbye!") return "Exit" if direction == "help": show. Introduction () return room if room == "Porch": if direction == "north": return "Entryway" if room == "Entryway": if direction == "north": return "Kitchen" if direction == "east": return "Living. Room" if direction == "south": return "Porch" return "Living. Room"

Rest of pick. Room() if room == Rest of pick. Room() if room == "Kitchen": if direction == "east": return "Dining. Room" if direction == "south": return "Entryway" if room == "Living. Room": if direction == "west": return "Entryway" if direction == "north": return "Dining. Room" if room == "Dining. Room": if direction == "west": return "Kitchen" if direction == "south": return "Living. Room"

Each room (function) describes itself def show. Porch(): print. Now( Each room (function) describes itself def show. Porch(): print. Now("You are on the porch of a frightening looking house. ") print. Now("The windows are broken. It's a dark and stormy night. ") print. Now("You can go north into the house. If you dare. ") def show. Entryway(): print. Now("You are in the entry way of the house. There are cobwebs in the corner. ") print. Now("You feel a sense of dread. ") print. Now("There is a passageway to the north and another to the east. ") print. Now("The porch is behind you to the south. ")

Running our program (so-far) >>> play. Game () Welcome to the Adventure House! In Running our program (so-far) >>> play. Game () Welcome to the Adventure House! In each room , you will be told which directions you can go. You can move north , south , east , or west by typing that direction. Type help to replay this introduction. Type quit or exit to end the program. You are on the porch of a frightening looking house.

Testing our program Try both expected, and unexpected input. We should return something reasonable Testing our program Try both expected, and unexpected input. We should return something reasonable in response to unreasonable input.

Returning a reasonable response to unreasonable pick. Room() input def pick. Room(direction , room Returning a reasonable response to unreasonable pick. Room() input def pick. Room(direction , room ): if (direction == "quit") or (direction == "exit"): … print. Now("Goodbye!") return "Exit" if direction == "help": show. Introduction () return room if room == "Dining. Room": if direction == "west": return "Kitchen" if direction == "south": return "Living. Room" print. Now("You can't (or don't want to) go in that direction. ") return room #Stay in current room

Now we handle unexpected input better >>> pick. Room('north ', 'Porch ') You can't Now we handle unexpected input better >>> pick. Room('north ', 'Porch ') You can't (or don't want to) go in that direction. 'Porch ' >>> pick. Room('Entryway ', 'Porch ') You can't (or don't want to) go in that direction. 'Porch '

Tips on Debugging Learn to trace code Print statements are your friends Don't be Tips on Debugging Learn to trace code Print statements are your friends Don't be afraid to change the program Use comments to “remove” parts temporarily when testing.

Seeing the Variables: show. Vars() Seeing the Variables: show. Vars()

Stepping through make. Sunset() with the Watcher Stepping through make. Sunset() with the Watcher

Improving the Adventure Game When testing, we discover: It's hard to tell which room Improving the Adventure Game When testing, we discover: It's hard to tell which room was which when playing the game. We can't figure out what we typed where.

Improving show. Room() def show. Room(room ): print. Now( Improving show. Room() def show. Room(room ): print. Now("======") if room == "Porch": show. Porch () if room == "Entryway": show. Entryway () if room == "Kitchen": show. Kitchen () if room == "Living. Room": show. LR () if room == "Dining. Room": show. DR ()

Improving play. Game() def play. Game (): location = Improving play. Game() def play. Game (): location = "Porch" show. Introduction () while not (location == "Exit") : show. Room(location) direction = request. String("Which direction? ") print. Now("You typed: "+direction) location = pick. Room(direction , location)

Better game play Better game play

Running programs outside of JES Once you make a larger program, you may want Running programs outside of JES Once you make a larger program, you may want to run it in Jython directly. 1. Import sys 2. Insert the JES sources into your sys. path 3. From media import * That's it!

How could we have done this differently? Store room descriptions and directions in a How could we have done this differently? Store room descriptions and directions in a list. Easier or harder to change in the future? Let each room “remember” (store in a data structure) where the exits and other rooms are. Easier or harder to change in the future? Let each room be an object that knows how to “show()” and “list. Exits()” Easier or harder to change in the future?

Let’s add an NPC ghost! Non-Player Character: Shows up in some rooms, and moves Let’s add an NPC ghost! Non-Player Character: Shows up in some rooms, and moves around. Create a ghost variable at the top of the file, before the play. Game function. ghost = 0 def play. Game(): location = "Porch" show. Introduction() while not (location == "Exit") : show. Room(location) direction = request. String("Which direction? ") print. Now("You typed: "+direction) location = pick. Room(direction, location)

Adding the ghost to the Entryway def show. Entryway(): global ghost print. Now( Adding the ghost to the Entryway def show. Entryway(): global ghost print. Now("You are in the entry way of the house. ") print. Now(" There are cobwebs in the corner. ") print. Now("You feel a sense of dread. ") if ghost == 0: print. Now("You suddenly feel cold. ") print. Now("You look up and see a thick mist. ") print. Now("It seems to be moaning. ") print. Now("Then it disappears. ") ghost = 1 print. Now("There is a passageway to the north and another to the east. ") print. Now("The porch is behind you to the south. ")

Adding the ghost to the kitchen def show. Kitchen(): global ghost print. Now( Adding the ghost to the kitchen def show. Kitchen(): global ghost print. Now("You are in the kitchen. ") print. Now("All the surfaces are covered with pots, ") print. Now(" pans, food pieces, and pools of blood. ") print. Now("You think you hear something up the stairs") print. Now(" that go up the west side of the room. ") print. Now("It's a scraping noise, like something being dragged") print. Now(" along the floor. ") if ghost == 1: print. Now("You see the mist you saw earlier. ") print. Now("But now it's darker, and red. ") print. Now("The moan increases in pitch and volume") print. Now(" so now it sounds more like a yell!") print. Now("Then it's gone. ") ghost = 0 print. Now("You can go to the south or east. ")