Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs.

Slides:



Advertisements
Similar presentations
PIIT Computer Science Summer Camp - Alice July 10, 2012 Brenda Parker Computer Science Department MTSU.
Advertisements

Chapter 9: Building Bigger Programs. Chapter Objectives.
CS0004: Introduction to Programming Repetition – Do Loops.
Basics of Computer Programming Web Design Section 8-1.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Chapter 2: Algorithm Discovery and Design
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Sentinel Logic Assumes while loops and input statements.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Systems Software Operating Systems.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Chapter 9 High-Level Programming Languages: C++. Chapter Goals Describe the expectations of high level languages Distinguish between functional design.
V Avon High School Tech Club Agenda Old Business –Delete Files New Business –Week 16 Topics: Intro to HTML/CSS –Questions? Tech Club Forums.
M1G Introduction to Programming 2 1. Designing a program.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Programming With Alice. Alice Free Java based, 3D programming tool Enables the manipulation and interaction of 3D objects Can also.
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 2.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
Testing Michael Ernst CSE 140 University of Washington.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Code reading skills LEVEL GAME.  Skeleton has error messages.  Notice the red lines on right slider. Click… you’ll go to an error.  pieces = levels.getPieces();
CIT 590 Intro to Programming Lecture 3. Pair programming logistics You and your partner submit 1 assignment Figure out who the submitter will be PLEASE.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Program Design and Debugging. How do programmers start? How do you get started with a program? “Programming is all about debugging a blank piece of paper.”
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Chapter 1 Software Engineering Principles. Problem analysis Requirements elicitation Software specification High- and low-level design Implementation.
Chapter 10: Building Bigger Programs. Chapter Objectives.
1.1: Objects and Classes msklug.weebly.com. Agenda: Attendance Let’s get started What is Java? Work Time.
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
Getting Started With Python Brendan Routledge
CS1315: Introduction to Media Computation Making sense of functions.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Chapter 6 Functions The Tic-Tac-Toe Game. Chapter Content In this chapter you will learn to do the following: 0 Write your own functions 0 Accept values.
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
Development Environment
PYGAME.
Chapter 10: Building Bigger Programs
Python - Loops and Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Week 8 - Programming II Today – more features: Loop control
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Introduction to TouchDevelop
CHAPTER 6: Control Flow Tools (for and while loops)
CS 177 Week 9 Recitation Slides
Chapter 9: Building Bigger Programs
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Chapter 4: Repetition Structures: Looping
Presentation transcript:

Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs

Chapter Objectives

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 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: – See Zork at ature=related ature=related – Play Zork at There are new and updated tools for making interactive fiction like Inform, fiction.orghttp:// fiction.org

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 printNow(): Takes a string as input, and prints it on the Command Area immediately. – Print waits until the program is done. requestString(): 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 “quit”? – A while loop repeats a block until a test becomes false.

Writing the top level function def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") location = pickRoom(direction, location) This function makes sense, even without knowing the lower level functions. It is decoupled from the lower- level. 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.

Writing the subfunctions def showIntroduction (): printNow("Welcome to the Adventure House!") printNow("In each room, you will be told which directions you can go.") printNow("You can move north, south, east, or west by typing that direction.") printNow("Type help to replay this introduction.") printNow("Type quit or exit to end the program.") def showRoom(room ): if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

pickRoom() def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room if room == "Porch": if direction == "north": return "Entryway" if room == "Entryway": if direction == "north": return "Kitchen" if direction == "east": return "LivingRoom" if direction == "south": return "Porch" return "LivingRoom"

Rest of pickRoom() if room == "Kitchen": if direction == "east": return "DiningRoom" if direction == "south": return "Entryway" if room == "LivingRoom": if direction == "west": return "Entryway" if direction == "north": return "DiningRoom" if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south":

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

Running our program (so-far) >>> playGame () 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 in response to unreasonable input.

Returning a reasonable response to unreasonable pickRoom() input def pickRoom(direction, room ): if (direction == "quit") or (direction == "exit"): printNow("Goodbye!") return "Exit" if direction == "help": showIntroduction () return room … if room == "DiningRoom": if direction == "west": return "Kitchen" if direction == "south": return "LivingRoom" printNow("You can’t (or don’t want to) go in that direction.") return room #Stay in current room

Now we handle unexpected input better >>> pickRoom(’north ’,’Porch ’) You can’t (or don’t want to) go in that direction. ’Porch ’ >>> pickRoom(’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 afraid to change the program – Use comments to “remove” parts temporarily when testing.

Seeing the Variables: showVars()

Stepping through makeSunset() with the Watcher

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 showRoom() def showRoom(room ): printNow("===========") if room == "Porch": showPorch () if room == "Entryway": showEntryway () if room == "Kitchen": showKitchen () if room == "LivingRoom": showLR () if room == "DiningRoom": showDR ()

Improving playGame() def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") printNow("You typed: "+direction) location = pickRoom(direction, location)

Better game play

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 “listExits()” – Easier or harder to change in the future?