Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012.

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Chapter 19, 20 Object Oriented Programming (OOP) Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Main task -write me a program
Black Jack Dr. Bernard Chen University of Central Arkansas Spring 2012.
PYTHON: LESSON 1 Catherine and Annie. WHAT IS PYTHON ANYWAY?  Python is a programming language.  But what’s a programming language?  It’s a language.
Structure of program You must start with a module import# You must then encapsulate any while loop in a main function at the start of the program Then.
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Chapter 9 IF Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Guide to Programming with Python Chapter Nine Working with/Creating Modules.
Python.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
An Introduction to Textual Programming
Hands on Projects Dr. Bernard Chen Ph.D. University of Central Arkansas July 9 th 2012
MOM! Phineas and Ferb are … Aims:
Guide to Programming with Python Chapter Three Branching, while Loops, and Program Planning: The Guess My Number Game.
Introduction to Python 2 Dr. Bernard Chen University of Central Arkansas PyArkansas 2011.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Modules. A module is a file containing Python definitions and statements intended for use in other Python programs. There are many modules as part of.
This Week The string type Modules print statement Writing programs if statements (time permitting) The boolean type (time permitting)
 Expression Tree and Objects 1. Elements of Python  Literals, Strings, Tuples, Lists, …  The order of file reading  The order of execution 2.
Module 3 Fraser High School. Module 3 – Loops and Booleans import statements - random use the random.randint() function use while loop write conditional.
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.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Ch. 10 For Statement Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Introduction to Computing Using Python for loop / def- ining a new function  Execution control structures ( if, for, function call)  def -ining a new.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Course A201: Introduction to Programming 09/16/2010.
Chapter Object Oriented Programming (OOP) CSC1310 Fall 2009.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Software Development. Software Development Loop Design  Programmers need a solid foundation before they start coding anything  Understand the task.
Decision Structures, String Comparison, Nested Structures
INLS 560 – F UNCTIONS Instructor: Jason Carter.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Python Let’s get started!.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Chapter 15. Modules Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
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.
Modules. Modules Modules are the highest level program organization unit, usually correspond to source files and serve as libraries of tools. Each file.
Review: A Structural View program modules -> main program -> functions -> libraries statements -> simple statements -> compound statements expressions.
Getting Started With Python Brendan Routledge
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.
Hardware & Software Lesson 1 Computer Systems & Components
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2012
Introduction to Programming
Line Continuation, Output Formatting, and Decision Structures
Computer Programming Fundamentals
Lesson 4 - Challenges.
Writing Functions( ) (Part 5)
Selection CIS 40 – Introduction to Programming in Python
Engineering 1020 Introduction to Programming
Functions and Procedures
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
Writing Functions( ) (Part 4)
Python programming exercise
Types, Truth, and Expressions (Part 2)
Python While Loops.
CISC101 Reminders Assignment 3 due today.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012

Outline Black Jack File I/O Module Class

Some major program skills you will use Loop If statement Function (So, basically, it will be a great practice on what you have learned so far om this semester)

Black Jack Game basic logics Initiation Loop Deliver two cards to player Player’s response Deliver two cards to house (if necessary) House’s response WIN or LOSE

Initiation import random print "Welcome to Black Jack Table!!" money=1000 print "Now you have $", money, "dollars" cards=[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8, 8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13] random.shuffle(cards)

Initiation Random Function: random.shuffle(cards)

Initiation (print out cards) def print_card(x): if x <10: print " " print "|",x," |" print "| |" print "| ",x," |" print " " else: print " " print "|",x," |" print "| |" print "| ",x,"|" print " " return

Initiation def card_add(sum, x): if x < 10: sum+=x else: sum+=10 return sum

Black Jack Game basic logics Initiation Loop Deliver two cards to player Player’s response Deliver two cards to house (if necessary) House’s response WIN or LOSE

Loop for i in range(10): chip_in = int(raw_input('How much money you want to play?')) player_sum=0 house_sum=0

Black Jack Game basic logics Initiation Loop Deliver two cards to player Player’s response Deliver two cards to house (if necessary) House’s response WIN or LOSE

Deliver two cards to player Player’s response if chip_in > 0 and money-chip_in >= 0: player_sum = card_add(player_sum, cards[-1]) print_card(cards.pop()) player_sum = card_add(player_sum, cards[-1]) print_card(cards.pop()) print "Your point:",player_sum

Deliver two cards to player Player’s response Build-in function cards.pop()

Deliver two cards to player Player’s response while (int(raw_input('Do you need an extra card? (1:yes, 0:no)'))): player_sum = card_add(player_sum, cards[-1]) print_card(cards.pop()) print "Your point:",player_sum if player_sum > 21: print "You lose!!!" money-=chip_in print "Now you have $", money, "dollars" break

Black Jack Game basic logics Initiation Loop Deliver two cards to player Player’s response Deliver two cards to house (if necessary) House’s response WIN or LOSE

Deliver two cards to house House’s response If player’s point is > 21, then house does not need to play anymore (House win) If player’s point == 21, then house does not need to play anymore (Player win) Otherwise, house needs to play

Deliver two cards to house House’s response if player_sum == 21: print "You Win!!!" money+=chip_in*2 print "Now you have $", money, "dollars"

Deliver two cards to house House’s response if player_sum < 21: print "Now, it's my turn..." house_sum = card_add(house_sum, cards[-1]) print_card(cards.pop()) house_sum = card_add(house_sum, cards[-1]) print_card(cards.pop()) print "House point:",house_sum

Deliver two cards to house House’s response while (house_sum < player_sum): house_sum = card_add(house_sum, cards[-1]) print_card(cards.pop()) print "House point:",house_sum

Black Jack Game basic logics Initiation Loop Deliver two cards to player Player’s response Deliver two cards to house (if necessary) House’s response WIN or LOSE

if house_sum = player_sum: print "You lose!!!" money-=chip_in print "Now you have $", money, "dollars" elif house_sum > 21: print "You Win!!!" money+=chip_in print "Now you have $", money, "dollars" else: print "You Win!!!" money+=chip_in print "Now you have $", money, "dollars"

Outline Black Jack File I/O Module Class

File Files: named storage compartment on your computer that are managed by operating system The built-in “open” function creates a Python file object, which serves as a link to a file in your computer

Read/Write file Open function take two variables, first on is the file name you want to deal with, another one is read or write of the file input = open ('file1.txt','r') Variable name Keyword file name read file output = open (‘output_file.txt’, ‘w’) Variable name Keyword file name write file

Read in File1

Read File1

Write file write number form 0 to 10 into a file output = open (‘output_file.txt’, ‘w’) for i in range(11): output.write(str(i)) output.write(‘\n’) output.close()

Read in File3

2D list: lists inside of list Here’s the way to create a 2D list (Just like what we saw last week) aa=[1,2,3] bb=[4,5,6] cc=[7,8,9] matrix=[] matrix.append(aa) matrix.append(bb) matrix.append(cc)

Read File3 input=open('file3.txt','r') matrix=[] for line in input.readlines(): matrix.append(line.split())

Print out the average score of each student input=open('file3.txt','r') matrix=[] for line in input.readlines(): matrix.append(line.split()) for i in range(len(matrix)): sum=0 for j in range(len(matrix[i])): sum+=int(matrix[i][j]) avg=sum/len(matrix[i]) print avg

Write the average score of each student to file input=open('file3.txt','r') output=open('avg.txt','w') matrix=[] for line in input.readlines(): matrix.append(line.split()) for i in range(len(matrix)): sum=0 for j in range(len(matrix[i])): sum+=int(matrix[i][j]) avg=sum/len(matrix[i]) print avg output.write(str(avg)+'\n') input.close() output.close()

Outline Black Jack File I/O Module Class

Modules Nodules are the highest level program organization unit, which packages program codes and data for reuse Actually, each “file” is a module (Look into Lib in Python)

Module Creation To define a module, use your text editor to type Python code into a text file You may create some functions or variables in that file You can call modules anything, but module filenames should end in.py suffix

Modules Usage Clients can use the module file we just wrote by running “import” statement >>> import math >>> import random >>> import module1 # assume this is the file name we saved

Modules examples We will create two modules: module1 and module2 module2 will import data and functions from module1

module1.py print “First line of module1” def print_out(aa): print aa*3 x=1 y=2

module2.py print “First line of module2” import module1 module1.print_out(“Hello World!! ”) # Use module1’s function print module1.x, module1.y # Reference module1’s variable x=10 y=20 print x, y

module2 output The result of execute this program is: Hello World!! Hello World!! Hello World!!

Outline Black Jack File I/O Module Class

What is OOP To qualify as being truly object-oriented objects generally need to also participate in something called an inheritance hierarchy Inheritance --- a mechanism of code customization and reuse, above and beyond anything we’ve seen so far

Class tree Two key words need to define: 1. Classes Serve as instance factory 2. Instances Represent the product generate from classes

Class tree

We usually call class higher in the tree (like c2 and c3) superclasses; classes lower in the tree (like c1) are known as subclasses The search procedure (try to look up some specific function belongs to which class) proceeds bottom-up, starting from left to right