Download presentation
Presentation is loading. Please wait.
Published byLester Webb Modified over 9 years ago
1
Final Review Dr. Bernard Chen University of Central Arkansas Spring 2012
2
Outline Black Jack File I/O Module Class
3
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)
4
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
5
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)
6
Initiation Random Function: random.shuffle(cards)
7
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
8
Initiation def card_add(sum, x): if x < 10: sum+=x else: sum+=10 return sum
9
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
10
Loop for i in range(10): chip_in = int(raw_input('How much money you want to play?')) player_sum=0 house_sum=0
11
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
12
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
13
Deliver two cards to player Player’s response Build-in function cards.pop()
14
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
15
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
16
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
17
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"
18
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
19
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
20
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
21
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"
22
Outline Black Jack File I/O Module Class
23
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
24
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
25
Read in File1
26
Read File1
27
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()
28
Read in File3
29
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)
30
Read File3 input=open('file3.txt','r') matrix=[] for line in input.readlines(): matrix.append(line.split())
31
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
32
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()
33
Outline Black Jack File I/O Module Class
34
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)
35
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
36
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
37
Modules examples We will create two modules: module1 and module2 module2 will import data and functions from module1
38
module1.py print “First line of module1” def print_out(aa): print aa*3 x=1 y=2
39
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
40
module2 output The result of execute this program is: Hello World!! Hello World!! Hello World!! 1 2 10 20
41
Outline Black Jack File I/O Module Class
42
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
43
Class tree Two key words need to define: 1. Classes Serve as instance factory 2. Instances Represent the product generate from classes
44
Class tree
45
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.