| | |
Projects for the Beginner
![]() |
Spreadsheets are really not that difficult to make with Python. Here is the beginning of a simple Tkinter based spreadsheet program:
Right now I am using the right mouse button click to evaluate equations starting with '=', for instance a cell data of '=355/113.0' would give the approximation of pi.
Still to do --> summing rows or columns of cells using something like '=sum(A1..A4)' needs to be parsed and evaluated. You also have to add a data save and load, plus a few more items you come up with. A nice project to complete!
python Syntax (Toggle Plain Text)
# the beginning of a simple Tkinter spreadsheet program import Tkinter as tk root = tk.Tk() root.title('Tkinter spreadsheet phase 1') def click(event, cell): # can do different things with right (3) and left (1) mouse button clicks root.title("you clicked mouse button %d in cell %s" % (event.num, cell)) # test right mouse button for equation solving # eg. data = '=9-3' would give 6 if event.num == 3: # entry object in use obj = dict[cell] # get data in obj data = obj.get() # if it starts with '=' it's an equation if data.startswith('='): eq = data.lstrip('=') print data, eq try: # solve the equation result = eval(eq) #print result, type(result) # test # remove equation data obj.delete(0, 'end') # update cell with equation result obj.insert(0, str(result)) except: pass def key_r(event, cell): # return/enter has been pressed data = dict[cell].get() # get text/data in given cell #print cell, dict[cell], data # test root.title("cell %s contains %s" % (cell, data)) # create a dictionary of key:value pairs dict = {} w = 20 h = 1 alpha = ["", 'A', 'B', 'C', 'D', 'E', 'F'] for row in range(5): for col in range(5): if col == 0: # create row labels label1 = tk.Label(root, width=3, text=str(row)) label1.grid(row=row, column=col, padx = 2, pady=2) elif row == 0: # create column labels label1 = tk.Label(root, width=w, text=alpha[col]) label1.grid(row=row, column=col, padx = 2, pady=2) else: # create entry object entry1 = tk.Entry(root, width=w) # place the object entry1.grid(row=row, column=col) #, padx = 2, pady=2) # create a dictionary of cell:object pair cell = "%s%s" % (alpha[col], row) dict[cell] = entry1 # bind the object to a left mouse click entry1.bind('<Button-1>', lambda e, cell=cell: click(e, cell)) # bind the object to a right mouse click entry1.bind('<Button-3>', lambda e, cell=cell: click(e, cell)) # bind the object to a return/enter press entry1.bind('<Return>', lambda e, cell=cell: key_r(e, cell)) #print dict # test # set the focus on cell A1 dict['A1'].focus() root.mainloop()
Still to do --> summing rows or columns of cells using something like '=sum(A1..A4)' needs to be parsed and evaluated. You also have to add a data save and load, plus a few more items you come up with. A nice project to complete!
Last edited by vegaseat; May 15th, 2007 at 9:02 pm. Reason: php tags not working, replaced them
drink her pretty
You have the following data statements:
Write a program that lists each person and whom they love, like and hate.
python Syntax (Toggle Plain Text)
data = """ Jim likes Larry and Jean, but hates Kim. Bob loves Jean, and likes Larry and Kim. Jean loves Bob, likes Jim, but hates Kim. Kim hates Jim, likes Larry and Bob. Larry loves Martin, and hates Karl and Jean. """
Last edited by Ene Uran; Feb 18th, 2007 at 6:24 pm.
drink her pretty
Am I authorised to post here?
This one should be nice-ish.
I'm sure you've heard of Sudoku? Well write a computer program that creates a sudoku game for the user. Try to be innovative in the program rules (tweak the sudoku game rules a little so that the program is fun to play, or maybe more challenging...nothing dratsic though that doesn't make it "sudoku" anymore).
I already started a very limited working console version that *tries* to generate a solved puzzle, and then plays the game by comparing the solved puzzle to a new grid created to store the user's input.
Scroll down to get the code...
Your objectives are simple(ah...not so much), and ranked by ease(the latter ones may really try you if you are a beginner):
1. Currently, the program generates a very limited set of very easily solved puzzles(there is an obvious pattern that each and every puzzle follows). You are required to change the program so that it can read a puzzle from a data file.
2. Create a Gui front end for this program and get rid of this ugly, tedious console prompt...after which, allow the user to input false/temporary entries in the available squares that they can erase at any time they choose. When you've done with that, to really polish your UI, allow the user the ability to undo a max of 10 moves by order in which they made them (this should be separate from the ability to delete temporary squares), HINT: use a static stack implementation (This isn't too tedious right?)
3. Create an algorithm so that the program will no longer check to see if the user grid matches the solved grid (to determine win), but check each of the nine blocks, rows and columns individually to detemine a win.
4. Create an algorithm that can generate more dynamic "Valid" puzzles.
Questions? PM me. If you need help, I think it's ok to post in the forums...but I'm no mod...
here is the code:
This one should be nice-ish.
I'm sure you've heard of Sudoku? Well write a computer program that creates a sudoku game for the user. Try to be innovative in the program rules (tweak the sudoku game rules a little so that the program is fun to play, or maybe more challenging...nothing dratsic though that doesn't make it "sudoku" anymore).
I already started a very limited working console version that *tries* to generate a solved puzzle, and then plays the game by comparing the solved puzzle to a new grid created to store the user's input.
Scroll down to get the code...
Your objectives are simple(ah...not so much), and ranked by ease(the latter ones may really try you if you are a beginner):
1. Currently, the program generates a very limited set of very easily solved puzzles(there is an obvious pattern that each and every puzzle follows). You are required to change the program so that it can read a puzzle from a data file.
2. Create a Gui front end for this program and get rid of this ugly, tedious console prompt...after which, allow the user to input false/temporary entries in the available squares that they can erase at any time they choose. When you've done with that, to really polish your UI, allow the user the ability to undo a max of 10 moves by order in which they made them (this should be separate from the ability to delete temporary squares), HINT: use a static stack implementation (This isn't too tedious right?)
3. Create an algorithm so that the program will no longer check to see if the user grid matches the solved grid (to determine win), but check each of the nine blocks, rows and columns individually to detemine a win.
4. Create an algorithm that can generate more dynamic "Valid" puzzles.
Questions? PM me. If you need help, I think it's ok to post in the forums...but I'm no mod...
here is the code:
python Syntax (Toggle Plain Text)
import random, time, sys NINE=[1, 2, 3, 4, 5, 6, 7, 8, 9] SIX=[1, 2, 3, 4, 5, 6] FOUR=[1, 2, 3, 4] def do_bottom(row, mode): if mode==9: row2=[] i=2 while len(row2)<7: row2.append(row[i]) i+=1 i=0 while i<2: row2.append(row[i]) i+=1 return row2 def do_top(row, mode): if mode==9: row2=[] i=1 while len(row2)<8: row2.append(row[i]) i+=1 i=0 while i<1: row2.append(row[i]) i+=1 return row2 def do_next(row, mode): if mode==9: row2=[] i=3 while len(row2)<6: row2.append(row[i]) i+=1 i=0 while i<3: row2.append(row[i]) i+=1 return row2 class Grid(object): def __init__(self, size): self.rows=[] self.size=size self.populate(size) self.solution=self.rows[:] self.erase(1) self.wrong=0 self.turns=0 def populate(self,mode): if mode==9: starter=NINE[:] random.shuffle(starter) self.row1=starter self.rows.append(self.row1) self.row2=do_next(starter, mode) self.rows.append(self.row2) self.row3=do_next(self.row2, mode) self.rows.append(self.row3) self.row4=do_top(starter, mode) self.rows.append(self.row4) self.row5=do_next(self.row4, mode) self.rows.append(self.row5) self.row6=do_next(self.row5, mode) self.rows.append(self.row6) self.row7=do_bottom(starter, mode) self.rows.append(self.row7) self.row8=do_next(self.row7, mode) self.rows.append(self.row8) self.row9=do_next(self.row8, mode) self.rows.append(self.row9) def print_me(self): print "\n\nColumns:1 2 3 | 4 5 6 | 7 8 9\t|Rows" print "_____________________________________________________" i=0 for row in self.rows: i+=1 if i ==4 or i==7: print "___________________________________________________", print "\n\t", j=0 for number in row: j+=1 if j==4 or j==7: print "| ", print str(number)+" ", print "\t|"+str(i) def print_gride(self): for row in self.solution: print "\n" for number in row: print str(number)+" ", def erase(self, mode): if mode==1: num=[] for i in range(9): nums=[] nums.append(random.randrange(3)) nums.append(random.randrange(3,6)) nums.append(random.randrange(6,9)) num.append(nums) i=0 for i in range(9): self.rows[i]=["x", "x", "x", "x", "x", "x", "x", "x", "x"] for number in num[i]: self.rows[i][number]=self.solution[i][number] def check_entries(self, row, column, entry): if self.rows[row][column]!="x": print "Sorry, this square has already been filled." self.turns-=1 elif self.solution[row][column]==entry: print "\nCorrect." time.sleep(2) self.rows[row][column]=entry else: print "\nNo. Bad entry. Correct entry is being inserted." self.rows[row][column]=self.solution[row][column] self.wrong+=1 time.sleep(2) def do_turn(self): self.print_me() self.turns+=1 print "\nWrong guesses: "+str(self.wrong)+"\tTurns: "+str(self.turns) time.sleep(3) raw_input("Press ENTER when you are ready to continue") print "Input the row number of the block you wish to guess." row=get_integer(1, 10) print "Input the column number." column=get_integer(1, 10) print "Now what number do you think should be in this block?" change=get_integer(1, 10) print "\nNow, are you absolutely sure about these entries?" print "1. Yes" print "2. No" sure=raw_input() if sure =="1": self.check_entries(row-1, column-1, change) else: self.turns-=1 print "Okay, think about it." time.sleep(2) def check(self): if self.wrong>=5: print "Sorry! Too many bad moves!\n\n\n" time.sleep(1) print""" GAME OVER - YOU LOST""" time.sleep(1) print """ reverting to main screen""" return False elif self.rows==self.solution: print "You solved it! You actually solved it." print "You conquered this puzzle in", self.turns, "turns, and only", wrong,"bad moves." time.sleep(1) print "\n\n\n" print """ GAME OVER - YOU WON""" time.sleep(1) print""" reverting to main screen""" return False else: return True def interface(): print "\n\n\t\t9 Puzzle Sudoku\n" print "What grid qould you like?" print "1. 4x4" print "2. 6x6" print "3. 9x9" print "\n\n4. No Grid. Let me out" option=raw_input() if option=="1" or option=="2": print "sorry, this function has not yet been coded." if option=="4": print "\t\t\tGOODBYE" time.sleep(3) sys.exit() if option=="3": print "\nCreating your puzzle, please wait." grid=Grid(9) run=1 while run: grid.do_turn() run=grid.check() interface() def get_integer(start, stop): try: x=int(raw_input()) if x in range(start, stop): return x else: y=int(asdsd) except: print "You must enter a number between", start,"and",stop-1 return get_integer(start, stop) interface()
If you are interested in mathematics, there is a large number of projects you can participate in at:
http://www.projecteuler.net/
You can use any language, but Python seems to be popular. Girls, don't be scared, mathematics is not just for the guys!
http://www.projecteuler.net/
You can use any language, but Python seems to be popular. Girls, don't be scared, mathematics is not just for the guys!
Just an exercise in drawing letters with the wxPython GUI toolkit:
python Syntax (Toggle Plain Text)
# a nifty 'Hello' using wxPython drawing stuff # (you can add 'World' as a coding exercise) import wx class MyFrame(wx.Frame): """a frame with a panel""" def __init__(self, parent=None, id=-1, title=None): wx.Frame.__init__(self, parent, id, title) self.panel = wx.Panel(self, size=(500, 200)) self.panel.Bind(wx.EVT_PAINT, self.on_paint) self.Fit() def on_paint(self, event): # establish the painting surface of given color dc = wx.PaintDC(self.panel) bg_color = 'yellow' dc.SetBrush(wx.Brush(bg_color)) dc.DrawRectangleRect((0, 0, 500, 200)) # draw 5 adjoining red circles with red fill dc.SetPen(wx.Pen('red', 1)) dc.SetBrush(wx.Brush('red')) y = 100 r = 50 for k in range(1, 6): x = k * 100 - 50 dc.DrawCircle(x, y, r) # cut out a few notches for the letters # draw background colored rectangles (x, y, w, h) dc.SetPen(wx.Pen(bg_color, 1)) dc.SetBrush(wx.Brush(bg_color)) dc.DrawRectangleRect((40, 50, 20, 40)) dc.DrawRectangleRect((40, 110, 20, 40)) dc.DrawRectangleRect((150, 70, 50, 20)) dc.DrawRectangleRect((150, 110, 50, 20)) dc.DrawRectangleRect((240, 50, 60, 70)) dc.DrawRectangleRect((340, 50, 60, 70)) dc.DrawCircle(450, 100, 20) app = wx.PySimpleApp() frame1 = MyFrame(title='the hello circles') frame1.Center() frame1.Show() app.MainLoop()
drink her pretty
Predict the outcome of this Python code:
Can you explain the odd behaviour?
python Syntax (Toggle Plain Text)
def sub_one(x): print "x before if -->", x if x > 1: x = x - 1 sub_one(x) print "x after if -->", x return x x = 5 final = sub_one(x) print "final result -->", final
No one died when Clinton lied.
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Solved Threads: 0
Module for handling strings (learning stuff)
Python Syntax (Toggle Plain Text)
####################################### # Programmed by : StrikerX # Purpose : Module for handling string ####################################### def toUpper(string): string_upper = "" for char in string : if ord(char) < 97 : string_upper += char elif ord(char) >= 97 : string_upper += chr(ord(char) - 32) print string_upper def toLower(string): string_Lower = "" for char in string : if ord(char) > 97 : string_Lower += char elif ord(char) <= 97: string_Lower += chr(ord(char) + 32 ) print string_Lower def swapCase(string): string_Swapped = "" for char in string : if ord(char) >= 97: string_Swapped += chr(ord(char) - 32 ) elif ord(char) < 97: string_Swapped += chr(ord(char) + 32 ) print string_Swapped def lenString(string): count = 0 for char in string : count += 1 return count def exist(char, string): if char in String : print "0" #True else : print "1" #False def startsWith(char, string): if string[0] == char : print "0" #True else : print "1" #False def endsWith(char, string): if string[len(string) - 1] == char: print "0" #True else : print "1" #False def index(string): print string[0] def capitalize(string): Str = "" char = '' x = string[0] if ord(string[0]) < 97 : char = string[0] if ord(string[0]) >= 97 : char = chr(ord(string[0]) - 32) Str = char + string[1:] print Str def printf(string): print string
•
•
Join Date: Apr 2007
Posts: 12
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
#!bin/python #-############################### # Programmer : StrikerX # Purpose : Cat Tool ################################## #cat.py state fileName #cat.py -h import sys import os def read(): fileName = sys.argv[2] f = open(fileName, "r") for line in f.readlines(): print line f.close() def append(): arr = [] fileName = sys.argv[2] if os.path.exists(fileName): f = open(fileName, "a") buf = "string" while buf != "": buf = raw_input("") arr.append(buf) for x in arr: f.write(x + '\n') f.close() def write(): fileName = sys.argv[2] arr = [] f = open(fileName, "w") buf = "string" while buf != "": buf = raw_input("") arr.append(buf) for x in arr: f.write(x + '\n') f.close() def h(): print "\tParam -> State" print "\n\t > -> Write\n\t>> -> Append\n\t-r -> read\n\t-a -> About\n\t-h -> Help Menu" param = sys.argv[1] if param == "-h": fileName = "" h() elif param == "-r": read() elif param == "-w": write() elif param == "-a": append() else: print "an illegal parameter !!"
Last edited by StrikerX; May 19th, 2007 at 10:10 pm.
![]() |
Similar Threads
- Ideal project for a beginner? (Python)
- Hep finding C++ Projects (Python)
- Help finding C++ Projects (C++)
- New task from Projects for the beginner: (Python)
Other Threads in the Python Forum
- Previous Thread: Wing IDE & IronPython
- Next Thread: Printing dictionary sorted by value
| Thread Tools | Search this Thread |
.so abrupt advice anti apax arax avogadro bash beginner book c# c++ calculator code college console convert coordinates csv cturtle curved def development edit embed enter erp event examples excel file forms function google gui halp hints homework http ideas iframe input introduction itconsulting itunes j2seprojects jaunty java javaprojects linux list lists loan maze microsoft mouse movingimageswithpygame number obexftp parameters path php prime programming project projects py2exe pygame pygtk pyopengl python random raw_input read redirect remote reverse silverlight simple softwaredevelopment string sudokusolver sum suthar syntax systemintegration terminal tkinter tlapse tricks tutorial ubuntu urllib urllib2 variable ventrilo web-scrape wordgame wxpython xlwt







