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

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

Jan 20, 2016

Download

Documents

Lester Webb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012.

Final Review

Dr. Bernard Chen

University of Central ArkansasSpring 2012

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

Outline Black Jack File I/O Module Class

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

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)

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

Black Jack Game basic logics

Initiation Loop

Deliver two cards to playerPlayer’s responseDeliver two cards to house (if

necessary)House’s responseWIN or LOSE

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

Initiation

import random

print "Welcome to Black Jack Table!!"money=1000print "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)

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

Initiation

Random Function:

random.shuffle(cards)

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

Initiation (print out cards)

def print_card(x): if x <10: print "-----------" print "|",x," |" print "| |" print "| |" print "| ",x," |" print "-----------" else: print "-----------" print "|",x," |" print "| |" print "| |" print "| ",x,"|" print "-----------" return

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

Initiation

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

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

Black Jack Game basic logics

Initiation Loop

Deliver two cards to playerPlayer’s responseDeliver two cards to house (if

necessary)House’s responseWIN or LOSE

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

Loop

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

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

Black Jack Game basic logics

Initiation Loop

Deliver two cards to playerPlayer’s responseDeliver two cards to house (if

necessary)House’s responseWIN or LOSE

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

Deliver two cards to playerPlayer’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

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

Deliver two cards to playerPlayer’s response

Build-in function

cards.pop()

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

Deliver two cards to playerPlayer’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

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

Black Jack Game basic logics

Initiation Loop

Deliver two cards to playerPlayer’s responseDeliver two cards to house (if

necessary)House’s responseWIN or LOSE

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

Deliver two cards to houseHouse’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

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

Deliver two cards to houseHouse’s response

if player_sum == 21: print "You Win!!!" money+=chip_in*2 print "Now you have $", money, "dollars"

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

Deliver two cards to houseHouse’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

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

Deliver two cards to houseHouse’s response

while (house_sum < player_sum): house_sum = card_add(house_sum, cards[-1]) print_card(cards.pop()) print "House point:",house_sum

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

Black Jack Game basic logics

Initiation Loop

Deliver two cards to playerPlayer’s responseDeliver two cards to house (if

necessary)House’s responseWIN or LOSE

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

WIN or LOSE

if house_sum<=21 and 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"

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

Outline Black Jack File I/O Module Class

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

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

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

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

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

Read in File1

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

Read File1

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

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

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

Read in File3

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

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)

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

Read File3

input=open('file3.txt','r')

matrix=[]for line in input.readlines(): matrix.append(line.split())

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

Print out the average score of each studentinput=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

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

Write the average score of each student to fileinput=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()

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

Outline Black Jack File I/O Module Class

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

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)

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

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

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

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

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

Modules examples

We will create two modules: module1 and module2

module2 will import data and functions from module1

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

module1.py

print “First line of module1”def print_out(aa):

print aa*3

x=1y=2

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

module2.pyprint “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=10y=20

print x, y

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

module2 output

The result of execute this program is:

Hello World!! Hello World!! Hello World!!

1 2 10 20

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

Outline Black Jack File I/O Module Class

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

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

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

Class tree

Two key words need to define:1. Classes

Serve as instance factory2. Instances

Represent the product generate from classes

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

Class tree

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

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