final review dr. bernard chen university of central arkansas spring 2012
TRANSCRIPT
Final Review
Dr. Bernard Chen
University of Central ArkansasSpring 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 playerPlayer’s responseDeliver two cards to house (if
necessary)House’s responseWIN or LOSE
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)
Initiation
Random Function:
random.shuffle(cards)
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
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 playerPlayer’s responseDeliver two cards to house (if
necessary)House’s responseWIN 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 playerPlayer’s responseDeliver two cards to house (if
necessary)House’s responseWIN or LOSE
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
Deliver two cards to playerPlayer’s response
Build-in function
cards.pop()
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
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
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
Deliver two cards to houseHouse’s response
if player_sum == 21: print "You Win!!!" money+=chip_in*2 print "Now you have $", money, "dollars"
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
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
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
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"
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 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
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()
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=1y=2
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
module2 output
The result of execute this program is:
Hello World!! Hello World!! Hello World!!
1 2 10 20
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 factory2. Instances
Represent the product generate from classes
Class tree
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