Chapter 10: Building Bigger Programs

Slides:



Advertisements
Similar presentations
Chapter 9: Building Bigger Programs. Chapter Objectives.
Advertisements

Chapter 1 - An Introduction to Computers and Problem Solving
CS0004: Introduction to Programming Repetition – Do Loops.
Basics of Computer Programming Web Design Section 8-1.
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Chapter 2: Algorithm Discovery and Design
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 9: Building Bigger Programs.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
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.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
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.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Python Let’s get started!.
Chapter 1 Software Engineering Principles. Problem analysis Requirements elicitation Software specification High- and low-level design Implementation.
Chapter 10: Building Bigger Programs. Chapter Objectives.
Problem-solving with Computers. 2Outline  Computer System  5 Steps for producing a computer program  Structured program and programming  3 types of.
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
Loop Structures and Booleans Zelle - Chapter 8 Charles Severance - Textbook: Python Programming: An Introduction to Computer Science,
PROBLEM SOLVING WARM-UP Fill in the spaces using any operation to solve the following (!, (), -/+,÷,×): = 6.
CSC 108H: Introduction to Computer Programming
Development Environment
Scratch for Interactivity
Basics of Computer Programming
PYGAME.
Python Let’s get started!.
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
Loops and Iteration Chapter 5 Python for Everybody
Python: Control Structures
Creativity in Algorithms
Computer Programming I
CMSC201 Computer Science I for Majors Lecture 13 – Midterm Review
Basics of Computer Programming
Basics of Computer Programming
Application Development Theory
Basics of Computer Programming
While Loops Chapter 3.
Python - Loops and Iteration
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Week 8 - Programming II Today – more features: Loop control
Writing Functions( ) (Part 5)
Call Stacks, Arguments and Returns
Learning to Program in Python
Sentinel logic, flags, break Taken from notes by Dr. Neil Moore
Loop Structures and Booleans Zelle - Chapter 8
Part B – Structured Exception Handling
Structured Programming Taken from notes by Dr. Neil Moore
Coding Concepts (Basics)
Introduction to TouchDevelop
Program Documentation
A look at Python Programming Language 2018.
CHAPTER 6: Control Flow Tools (for and while loops)
ICT Gaming Lesson 2.
CS 177 Week 9 Recitation Slides
Chapter 9: Building Bigger Programs
Introduction to Computer Science
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Chapter 4: Repetition Structures: Looping
Python Inputs Mr. Husch.
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Lecture 6 - Recursion.
Presentation transcript:

Chapter 10: Building Bigger Programs Introduction to Computing and Programming in Python: A Multimedia Approach 4ed Chapter 10: Building Bigger Programs Thanks to John Sanders of Suffolk University for contributions to these slides!

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: http://en.wikipedia.org/wiki/Colossal_Cave_Adventure See Zork at https://youtu.be/1q9Q2gwqw7U Play Zork at http://textadventures.co.uk/games/view/5zyoqrsugeopel3ffhz_vq/zork There are new and updated tools for making interactive fiction like Inform, http://www.inform-fiction.org

Example Top-Down Design: An Adventure Game Top-level function: Tell the user how to play the game. Describe the room. Get the player's command. Figure out the next room. 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 This function makes sense, even without knowing the lower level functions. It is decoupled from the lower-level. Working directly from our earlier outline. def playGame (): location = "Porch" showIntroduction () while not (location == "Exit") : showRoom(location) direction = requestString("Which direction?") location = pickRoom(direction , location)

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 ()

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": return "Kitchen" if direction == "east": return "LivingRoom" if direction == "south": return "Porch" pickRoom()

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

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. >>> pickRoom("north", "Porch") 'Entryway' >>> pickRoom("north", "Entryway") 'Kitchen' >>> pickRoom("south", "Porch") >>> pickRoom("Entryway", "Porch") 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("south", "Porch") You can't (or don't want to) go in that direction. 'Porch ' >>> pickRoom("Entryway", "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.

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