In the terminal you can run your python programs:
Go To Applications->Accessories->Terminal
python yourprogramhere.py
Note you need to be in the same directory as your python program, otherwise you need to type:
python /fulldirectorypath/yourprogramhere.py
or
python ./remainingdirectorypath/yourprogramhere.py
if you only type python at the prompt then you enter into the python program where it expects you to code in python commands at that point and time. Don't do this instead code your python programs in the text editor and run them with the python command.
Tip you can cycle through your previously typed commands in Linux by hitting the up and down arrows on the keyboard at the prompt.
Go To Applications->Accessories->Text Editor
in the Text Editor go to View->Highlight Mode->Scripts->Python
in the Text Editor go to Edit->Preferences
in Preferences make sure the following is checked:
in the View Tab
Text Wrapping All Options Checked
Display Line Numbers Checked
Highlight Current Line Checked
Display Right Margin Checked and leave it at 80
Highlight Matching Bracket Checked
in the Editor
Tab Width leave it at 8
Insert Spaces Instead of Tabs DO NOT CHECK!!!! This will keep your python programs from running correctly do to the fact that it relies on tabs for the begin and ending parts of code blocks such as functions, condition statements, etc.
Enable Automatic Indentation Checked.
File Saving Check All
in the Font & Color Tab
Use The System Fixed Width Font Do Not Check.
Now you can select a larger font which will make it easy to read your code and debug faster.
Now you are ready to code your program in Python in the text editor and save your file with the extension .py
After you save your file test the program in the terminal with your python command as explained up above. If you make a syntax error you will get detailed messages of what the problem is and what line the error is on. The error messages are better in the terminal than they are in Idle.
Thursday, March 19, 2009
Monday, March 2, 2009
Wednesday, February 18, 2009
GUI Tic Tac Toe
Extra Credit:
Checkout chapter 10 in Python Programming for the Absolute Beginner.

# GUI Tic Tac Toe
from Tkinter import *
import sys
X = "X"
O = "O"
EMPTY = ""
TIE = "TIE"
NUM_SQUARES = 9
# YOUR TIC TAC TOE CODE GOES HERE
board = new_board()
class Application(Frame):
# GUI for the Main Window for Tic Tac Toe Game
def __init__(self,master): # Constructor Class Definition
Frame.__init__(self,master)
self.grid()
self.player1=X
self.player2=O
self.turn=X
self.CreateWidgets()
# Button Click Events
def BtnTopLeftClick(self):
legal = legal_moves(board)
move = 0
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnTopLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnTopMidClick(self):
legal = legal_moves(board)
move = 1
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopMid["text"] = "X"
else:
board[move] = self.player2
self.BtnTopMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnTopRightClick(self):
legal = legal_moves(board)
move = 2
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopRight["text"] = "X"
else:
board[move] = self.player2
self.BtnTopRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidLeftClick(self):
legal = legal_moves(board)
move = 3
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnMidLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidMidClick(self):
legal = legal_moves(board)
move = 4
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidMid["text"] = "X"
else:
board[move] = self.player2
self.BtnMidMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidRightClick(self):
legal = legal_moves(board)
move = 5
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidRight["text"] = "X"
else:
board[move] = self.player2
self.BtnMidRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotLeftClick(self):
legal = legal_moves(board)
move = 6
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnBotLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotMidClick(self):
legal = legal_moves(board)
move = 7
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotMid["text"] = "X"
else:
board[move] = self.player2
self.BtnBotMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotRightClick(self):
legal = legal_moves(board)
move = 8
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotRight["text"] = "X"
else:
board[move] = self.player2
self.BtnBotRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def CreateWidgets(self):
# Tic Tac Toe Buttons
self.BtnTopLeft = Button(self)
self.BtnTopLeft["text"] = " "
self.BtnTopLeft.grid(row=0,column=0,columnspan=1,sticky=W)
self.BtnTopLeft["command"]=self.BtnTopLeftClick
self.BtnTopMid = Button(self)
self.BtnTopMid["text"] = " "
self.BtnTopMid.grid(row=0,column=1,columnspan=1,sticky=W)
self.BtnTopMid["command"]=self.BtnTopMidClick
self.BtnTopRight = Button(self)
self.BtnTopRight["text"] = " "
self.BtnTopRight.grid(row=0,column=2,columnspan=1,sticky=W)
self.BtnTopRight["command"]=self.BtnTopRightClick
self.BtnMidLeft = Button(self)
self.BtnMidLeft["text"] = " "
self.BtnMidLeft.grid(row=1,column=0,columnspan=1,sticky=W)
self.BtnMidLeft["command"]=self.BtnMidLeftClick
self.BtnMidMid = Button(self)
self.BtnMidMid["text"] = " "
self.BtnMidMid.grid(row=1,column=1,columnspan=1,sticky=W)
self.BtnMidMid["command"]=self.BtnMidMidClick
self.BtnMidRight = Button(self)
self.BtnMidRight["text"] = " "
self.BtnMidRight.grid(row=1,column=2,columnspan=1,sticky=W)
self.BtnMidRight["command"]=self.BtnMidRightClick
self.BtnBotLeft = Button(self)
self.BtnBotLeft["text"] = " "
self.BtnBotLeft.grid(row=2,column=0,columnspan=1,sticky=W)
self.BtnBotLeft["command"]=self.BtnBotLeftClick
self.BtnBotMid = Button(self)
self.BtnBotMid["text"] = " "
self.BtnBotMid.grid(row=2,column=1,columnspan=1,sticky=W)
self.BtnBotMid["command"]=self.BtnBotMidClick
self.BtnBotRight = Button(self)
self.BtnBotRight["text"] = " "
self.BtnBotRight.grid(row=2,column=2,columnspan=1,sticky=W)
self.BtnBotRight["command"]=self.BtnBotRightClick
# main
def main():
player1Name=""
player2Name=""
player1Score=0
player2Score=0
namesScores=[]
player1Name,player2Name,player1Score,player2Score,namesScores=readFileNamesScores(player1Name,player2Name,player1Score,player2Score)
root= Tk()
root.title("Tic Tac Toe")
root.geometry("200x100")
app = Application(root)
root.mainloop()
main()
Checkout chapter 10 in Python Programming for the Absolute Beginner.
# GUI Tic Tac Toe
from Tkinter import *
import sys
X = "X"
O = "O"
EMPTY = ""
TIE = "TIE"
NUM_SQUARES = 9
# YOUR TIC TAC TOE CODE GOES HERE
board = new_board()
class Application(Frame):
# GUI for the Main Window for Tic Tac Toe Game
def __init__(self,master): # Constructor Class Definition
Frame.__init__(self,master)
self.grid()
self.player1=X
self.player2=O
self.turn=X
self.CreateWidgets()
# Button Click Events
def BtnTopLeftClick(self):
legal = legal_moves(board)
move = 0
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnTopLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnTopMidClick(self):
legal = legal_moves(board)
move = 1
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopMid["text"] = "X"
else:
board[move] = self.player2
self.BtnTopMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnTopRightClick(self):
legal = legal_moves(board)
move = 2
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnTopRight["text"] = "X"
else:
board[move] = self.player2
self.BtnTopRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidLeftClick(self):
legal = legal_moves(board)
move = 3
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnMidLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidMidClick(self):
legal = legal_moves(board)
move = 4
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidMid["text"] = "X"
else:
board[move] = self.player2
self.BtnMidMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnMidRightClick(self):
legal = legal_moves(board)
move = 5
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnMidRight["text"] = "X"
else:
board[move] = self.player2
self.BtnMidRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotLeftClick(self):
legal = legal_moves(board)
move = 6
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotLeft["text"] = "X"
else:
board[move] = self.player2
self.BtnBotLeft["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotMidClick(self):
legal = legal_moves(board)
move = 7
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotMid["text"] = "X"
else:
board[move] = self.player2
self.BtnBotMid["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def BtnBotRightClick(self):
legal = legal_moves(board)
move = 8
if move not in legal:
print "\nThat square is already occupied. Please Choose another one.\n"
else:
if self.turn == self.player1:
board[move] = self.player1
self.BtnBotRight["text"] = "X"
else:
board[move] = self.player2
self.BtnBotRight["text"] = "O"
self.turn = next_turn(self.turn)
if winner(board):
the_winner = winner(board)
congrat_winner(the_winner,self.player2,self.player1,player1Name,player2Name,player1Score,player2Score,namesScores)
def CreateWidgets(self):
# Tic Tac Toe Buttons
self.BtnTopLeft = Button(self)
self.BtnTopLeft["text"] = " "
self.BtnTopLeft.grid(row=0,column=0,columnspan=1,sticky=W)
self.BtnTopLeft["command"]=self.BtnTopLeftClick
self.BtnTopMid = Button(self)
self.BtnTopMid["text"] = " "
self.BtnTopMid.grid(row=0,column=1,columnspan=1,sticky=W)
self.BtnTopMid["command"]=self.BtnTopMidClick
self.BtnTopRight = Button(self)
self.BtnTopRight["text"] = " "
self.BtnTopRight.grid(row=0,column=2,columnspan=1,sticky=W)
self.BtnTopRight["command"]=self.BtnTopRightClick
self.BtnMidLeft = Button(self)
self.BtnMidLeft["text"] = " "
self.BtnMidLeft.grid(row=1,column=0,columnspan=1,sticky=W)
self.BtnMidLeft["command"]=self.BtnMidLeftClick
self.BtnMidMid = Button(self)
self.BtnMidMid["text"] = " "
self.BtnMidMid.grid(row=1,column=1,columnspan=1,sticky=W)
self.BtnMidMid["command"]=self.BtnMidMidClick
self.BtnMidRight = Button(self)
self.BtnMidRight["text"] = " "
self.BtnMidRight.grid(row=1,column=2,columnspan=1,sticky=W)
self.BtnMidRight["command"]=self.BtnMidRightClick
self.BtnBotLeft = Button(self)
self.BtnBotLeft["text"] = " "
self.BtnBotLeft.grid(row=2,column=0,columnspan=1,sticky=W)
self.BtnBotLeft["command"]=self.BtnBotLeftClick
self.BtnBotMid = Button(self)
self.BtnBotMid["text"] = " "
self.BtnBotMid.grid(row=2,column=1,columnspan=1,sticky=W)
self.BtnBotMid["command"]=self.BtnBotMidClick
self.BtnBotRight = Button(self)
self.BtnBotRight["text"] = " "
self.BtnBotRight.grid(row=2,column=2,columnspan=1,sticky=W)
self.BtnBotRight["command"]=self.BtnBotRightClick
# main
def main():
player1Name=""
player2Name=""
player1Score=0
player2Score=0
namesScores=[]
player1Name,player2Name,player1Score,player2Score,namesScores=readFileNamesScores(player1Name,player2Name,player1Score,player2Score)
root= Tk()
root.title("Tic Tac Toe")
root.geometry("200x100")
app = Application(root)
root.mainloop()
main()
Monday, January 26, 2009
Getting Linux Distrobutions
Linux Distros:
Ubuntu
If you have an extra hard drive that you can put in your computer than this is a great distro of linux to learn on for the desktop. It is super easy to use and update.
Mandriva
Mandriva One for the linux that runs off of the CD that I am using in class.
Burning OS Images to a Disk on Windows for Free:
ImgBurn
Cheap Linux Distrobutions
If you can not get your linux disk to boot from your computer when it is in the CD/DVD drive after you have restarted the computer then you will need to go into your BIOS and setup the boot sequence to where the CD/DVD drive is before the hard drive in the boot sequence order.
1. CD/DVD
2. Hard Drive
Then save your BIOS configuration and exit the BIOS setup screen. Then restart your computer and your computer will boot off of the CD/DVD that has linux on it.
Ubuntu
If you have an extra hard drive that you can put in your computer than this is a great distro of linux to learn on for the desktop. It is super easy to use and update.
Mandriva
Mandriva One for the linux that runs off of the CD that I am using in class.
Burning OS Images to a Disk on Windows for Free:
ImgBurn
Cheap Linux Distrobutions
If you can not get your linux disk to boot from your computer when it is in the CD/DVD drive after you have restarted the computer then you will need to go into your BIOS and setup the boot sequence to where the CD/DVD drive is before the hard drive in the boot sequence order.
1. CD/DVD
2. Hard Drive
Then save your BIOS configuration and exit the BIOS setup screen. Then restart your computer and your computer will boot off of the CD/DVD that has linux on it.
Monday, January 5, 2009
Virtual Library Python Programming For The Absolute Beginner
Python Programming For The Absolute Beginner for the Virtual Library so you don't have to spend any money. This is the first edition instead of the second edition like they have on Amazon. So checkout your virtual library.
Evil Message
import sys
import math
def evilmsg(person):
print "This computer is going to eat %s" % person
return
person=input("Enter your name here: ")
evilmsg(person)
stuff=input("Hit a key: ")
import math
def evilmsg(person):
print "This computer is going to eat %s" % person
return
person=input("Enter your name here: ")
evilmsg(person)
stuff=input("Hit a key: ")
Enter A Number
import sys
import math
def add(num1, num2):
sum=num1+num2
return sum
num1=input("Enter a number here: ")
num2=input("Enter a number here: ")
sum=add(num1,num2)
print "The sum of %d" % num1
import math
def add(num1, num2):
sum=num1+num2
return sum
num1=input("Enter a number here: ")
num2=input("Enter a number here: ")
sum=add(num1,num2)
print "The sum of %d" % num1
Subscribe to:
Posts (Atom)