Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.

Slides:



Advertisements
Similar presentations
Lesson 6: Boolean and If Statements Computer Science 1 Mr. Bernstein.
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Introduction to Macromedia Director 8.5 – Lingo
Programming Patterns CSC 161: The Art of Programming Prof. Henry Kautz 9/30/2009.
ECS 15 if and random. Topic  Testing user input using if statements  Truth and falsehood in Python  Getting random numbers.
Making Decisions in Python Sec 9-10 Web Design. Objectives The student will: Understand how to make a decision in Python Understand the structure of an.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Python (yay!) November 16, Unit 7. Recap We can store values in variables using an assignment statement >>>x = We can get input from the user using.
Program Design and Development
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.
Python Control of Flow.
Fundamentals of Python: From First Programs Through Data Structures
index.php Palmyra Area High School 1.
The University of Texas – Pan American
Fundamentals of Python: First Programs
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
ECS 10 10/8. Outline Announcements Homework 2 questions Boolean expressions If/else statements State variables and avoiding sys.exit(…) Example: Coin.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
If statements while loop for loop
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
For loops in programming Assumes you have seen assignment statements and print statements.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
Course A201: Introduction to Programming 09/16/2010.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
CHAPTER 3 Decisions and Repetition. A few new constants Constants so far:  Strings  Numbers  integer  float Boolean constants: [On board]
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Logical Operators, Boolean Variables, Random Numbers This template was just too good to let go in one day!
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Random Numbers Random numbers are extremely useful: especially for games, and also for calculating experimental probabilities. Formula for generating random.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
7 - Programming 7J, K, L, M, N, O – Handling Data.
Topic: Iterative Statements – Part 1 -> for loop
Week of 12/12/16 Test Review.
Lesson 4 - Challenges.
Lesson 05: Iterations Class Chat: Attendance: Participation
ECS10 10/10
Looping and Random Numbers
Python - Iteration Iteration
Iterations Programming Condition Controlled Loops (WHILE Loop)
Teaching KS3 Computing Session 4 Theory: Boolean logic AND/OR/NOT
Introduction to Python
Introduction to Python
Computers & Programming Languages
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
IST256 : Applications Programming for Information Systems
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Computer Science Core Concepts
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Console.WriteLine(“Good luck!”);
General Condition Loop
Another Example Problem
Types, Truth, and Expressions
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Hint idea 2 Split into shorter tasks like this.
Starter Look at the hand-out.
COMPUTING.
Presentation transcript:

Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009

Today's Lesson Don't panic! Top-down design Summary of the Elements of Python Don't panic! 2

Nim Two player game. Start with a pile of N marbles. Take turns taking 1, 2, or 3 marbles from the pile. Player who is forced to take the last marble loses. 3

1: State the Task Play a game of Nim against a human opponent, using the rules: Start with a pile of N marbles. Take turns taking 1, 2, or 3 marbles from the pile. Player who is forced to take the last marble loses. 4

2: The Three Main Stages Every task has a beginning, middle, and end. Beginning: Initialize Middle: Perform some computing Often involves repeating sub-tasks End: Compute final results Nim Start with a pile of N marbles. Get initial number of marbles N. Take turns taking 1, 2, or 3 marbles from the pile. Player who is forced to take the last marble loses. Output winner 5

3: Determine Stopping Condition for Middle Get initial number of marbles N. Take turns taking 1, 2, or 3 marbles from the pile. While game is not over: First player takes a turn If game is still not over, second player takes a turn Output winner. Need to determine who is first and second player! 6

Discovered Need to Add to Problem Specification Get initial number of marbles N. Take turns taking 1, 2, or 3 marbles from the pile. While game is not over: Human player takes a turn If game is still not over, Computer player takes a turn Output winner. 7

4: Break Down the Sub-Tasks in the Main Loop Get initial number of marbles N. While game is not over: Human player takes a turn Input a 1, 2, or 3 from Human Deduct that many marbles If game is still not over, Computer player takes a turn If game is not over: Compute how many marbles to take Deduct that many marbles Output winner. 8

5: Find Sub-Tasks that Need to be Broken Down Further Get initial number of marbles N. While game is not over: Human player takes a turn Input a 1, 2, or 3 from Human Deduct that many marbles If game is still not over, Computer player takes a turn If game is not over: Compute how many marbles to take Deduct that many marbles Output winner. 9

5: Find Sub-Tasks that Need to be Broken Down Further Get initial number of marbles N. While game is not over: Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: Compute how many marbles to take If 1 marble left, then take it Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles Output winner. 10

6: Turn into Python in Top- Down Fashion Get initial number of marbles N. While game is not over: Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left, then take it. Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles Output winner. 11

6: Turn in Python in Top-Down Fashion N = input('How many marbles? ') While game is not over: Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left, then take it. Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles Output winner. 12

6: Turn in Python in Top-Down Fashion N = input('How many marbles to start? ') while (N > 0): Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left, then take it. Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles Output winner. 13

6: Turn in Python in Top-Down Fashion N = input('How many marbles to start? ') while (N > 0): Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left, take it Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles Output winner. How will we know this? 14

Discovered Need to Introduce a New Variable N = input('How many marbles to start? ') while (N > 0): Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left: Number to take is 1 Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles if HumanWins: Print "You win!" else: Print "You lose!" 15

Initialize & Set New Variable Appropriately N = input('How many marbles to start? ') HumanWins = False while (N > 0): Input a 1, 2, or 3 from Human Deduct that many marbles If game is not over: If 1 marble left: Number to take is 1 HumanWins = True Else if 2 to 4 marbles are left: Compute number needed for Computer to win Else: Compute a random number from 1 to 3 Deduct that many marbles if HumanWins: Print "You win!" else: Print "You lose!" 16

Finish Turning into Python N = input('How many marbles to start? ') HumanWins = False while (N > 0): H = input('How many marbles do you want to take?') N = N – H if (N > 0): if (N == 1): C = 1 HumanWins = True elif N >= 2 and N <=4: C = N – 1 else: C = random.randint(1,3) N = N – C if HumanWins: Print "You win!" else: Print "You lose!" 17

7: Review for Correctness and Completeness Program should tell player how many marbles are left Program should tell player how many marbles it is taking 18

8: Make Final Changes N = input('How many marbles to start? ') HumanWins = False while (N > 0): print 'Number of marbles left is ', N H = input('How many marbles do you want to take?') N = N – H if (N > 0): if (N == 1): C = 1 HumanWins = True elif N >= 2 and N <=4: C = N – 1 else: C = random.randint(1,3) N = N – C print "Computer takes ', C if HumanWins: Print "You win!" else: Print "You lose!" 19

Elements of Python The key to learning any language is to become comfortable with a small, core vocabulary Basic data types: Integers Floating point numbers Truth-values (Booleans) Expressions Variables Operators Functions 20

Elements of Python Data Collections Lists Strings Sequence Operations (for Lists or Strings) String Library 21

Elements of Python Statements Assignment Loops While For If (conditional execution) Input Output User defined functions 22