Projects for the Beginner

Reply

Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is online now Online
Posting Virtuoso

Re: Projects for the Beginner

 
0
  #111
Feb 13th, 2007
Spreadsheets are really not that difficult to make with Python. Here is the beginning of a simple Tkinter based spreadsheet program:
  1. # the beginning of a simple Tkinter spreadsheet program
  2.  
  3. import Tkinter as tk
  4.  
  5. root = tk.Tk()
  6. root.title('Tkinter spreadsheet phase 1')
  7.  
  8. def click(event, cell):
  9. # can do different things with right (3) and left (1) mouse button clicks
  10. root.title("you clicked mouse button %d in cell %s" % (event.num, cell))
  11. # test right mouse button for equation solving
  12. # eg. data = '=9-3' would give 6
  13. if event.num == 3:
  14. # entry object in use
  15. obj = dict[cell]
  16. # get data in obj
  17. data = obj.get()
  18. # if it starts with '=' it's an equation
  19. if data.startswith('='):
  20. eq = data.lstrip('=')
  21. print data, eq
  22. try:
  23. # solve the equation
  24. result = eval(eq)
  25. #print result, type(result) # test
  26. # remove equation data
  27. obj.delete(0, 'end')
  28. # update cell with equation result
  29. obj.insert(0, str(result))
  30. except:
  31. pass
  32.  
  33. def key_r(event, cell):
  34. # return/enter has been pressed
  35. data = dict[cell].get() # get text/data in given cell
  36. #print cell, dict[cell], data # test
  37. root.title("cell %s contains %s" % (cell, data))
  38.  
  39. # create a dictionary of key:value pairs
  40. dict = {}
  41. w = 20
  42. h = 1
  43. alpha = ["", 'A', 'B', 'C', 'D', 'E', 'F']
  44. for row in range(5):
  45. for col in range(5):
  46. if col == 0:
  47. # create row labels
  48. label1 = tk.Label(root, width=3, text=str(row))
  49. label1.grid(row=row, column=col, padx = 2, pady=2)
  50. elif row == 0:
  51. # create column labels
  52. label1 = tk.Label(root, width=w, text=alpha[col])
  53. label1.grid(row=row, column=col, padx = 2, pady=2)
  54. else:
  55. # create entry object
  56. entry1 = tk.Entry(root, width=w)
  57. # place the object
  58. entry1.grid(row=row, column=col) #, padx = 2, pady=2)
  59. # create a dictionary of cell:object pair
  60. cell = "%s%s" % (alpha[col], row)
  61. dict[cell] = entry1
  62. # bind the object to a left mouse click
  63. entry1.bind('<Button-1>', lambda e, cell=cell: click(e, cell))
  64. # bind the object to a right mouse click
  65. entry1.bind('<Button-3>', lambda e, cell=cell: click(e, cell))
  66. # bind the object to a return/enter press
  67. entry1.bind('<Return>', lambda e, cell=cell: key_r(e, cell))
  68.  
  69. #print dict # test
  70.  
  71. # set the focus on cell A1
  72. dict['A1'].focus()
  73.  
  74. root.mainloop()
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!
Last edited by vegaseat; May 15th, 2007 at 9:02 pm. Reason: php tags not working, replaced them
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is online now Online
Posting Virtuoso

Re: Projects for the Beginner

 
0
  #112
Feb 18th, 2007
You have the following data statements:
  1. data = """
  2. Jim likes Larry and Jean, but hates Kim.
  3. Bob loves Jean, and likes Larry and Kim.
  4. Jean loves Bob, likes Jim, but hates Kim.
  5. Kim hates Jim, likes Larry and Bob.
  6. Larry loves Martin, and hates Karl and Jean.
  7. """
Write a program that lists each person and whom they love, like and hate.
Last edited by Ene Uran; Feb 18th, 2007 at 6:24 pm.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,602
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Projects for the Beginner

 
0
  #113
Mar 1st, 2007
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:
  1. import random, time, sys
  2. NINE=[1, 2, 3, 4, 5, 6, 7, 8, 9]
  3. SIX=[1, 2, 3, 4, 5, 6]
  4. FOUR=[1, 2, 3, 4]
  5. def do_bottom(row, mode):
  6. if mode==9:
  7. row2=[]
  8. i=2
  9. while len(row2)<7:
  10. row2.append(row[i])
  11. i+=1
  12. i=0
  13. while i<2:
  14. row2.append(row[i])
  15. i+=1
  16. return row2
  17.  
  18. def do_top(row, mode):
  19. if mode==9:
  20. row2=[]
  21. i=1
  22. while len(row2)<8:
  23. row2.append(row[i])
  24. i+=1
  25. i=0
  26. while i<1:
  27. row2.append(row[i])
  28. i+=1
  29. return row2
  30. def do_next(row, mode):
  31. if mode==9:
  32. row2=[]
  33. i=3
  34. while len(row2)<6:
  35. row2.append(row[i])
  36. i+=1
  37. i=0
  38. while i<3:
  39. row2.append(row[i])
  40. i+=1
  41. return row2
  42. class Grid(object):
  43. def __init__(self, size):
  44. self.rows=[]
  45. self.size=size
  46. self.populate(size)
  47. self.solution=self.rows[:]
  48. self.erase(1)
  49. self.wrong=0
  50. self.turns=0
  51.  
  52. def populate(self,mode):
  53. if mode==9:
  54. starter=NINE[:]
  55. random.shuffle(starter)
  56. self.row1=starter
  57. self.rows.append(self.row1)
  58. self.row2=do_next(starter, mode)
  59. self.rows.append(self.row2)
  60. self.row3=do_next(self.row2, mode)
  61. self.rows.append(self.row3)
  62. self.row4=do_top(starter, mode)
  63. self.rows.append(self.row4)
  64. self.row5=do_next(self.row4, mode)
  65. self.rows.append(self.row5)
  66. self.row6=do_next(self.row5, mode)
  67. self.rows.append(self.row6)
  68. self.row7=do_bottom(starter, mode)
  69. self.rows.append(self.row7)
  70. self.row8=do_next(self.row7, mode)
  71. self.rows.append(self.row8)
  72. self.row9=do_next(self.row8, mode)
  73. self.rows.append(self.row9)
  74. def print_me(self):
  75. print "\n\nColumns:1 2 3 | 4 5 6 | 7 8 9\t|Rows"
  76. print "_____________________________________________________"
  77. i=0
  78. for row in self.rows:
  79. i+=1
  80. if i ==4 or i==7:
  81. print "___________________________________________________",
  82. print "\n\t",
  83. j=0
  84. for number in row:
  85. j+=1
  86. if j==4 or j==7:
  87. print "| ",
  88. print str(number)+" ",
  89. print "\t|"+str(i)
  90. def print_gride(self):
  91. for row in self.solution:
  92. print "\n"
  93. for number in row:
  94. print str(number)+" ",
  95. def erase(self, mode):
  96. if mode==1:
  97. num=[]
  98. for i in range(9):
  99. nums=[]
  100. nums.append(random.randrange(3))
  101. nums.append(random.randrange(3,6))
  102. nums.append(random.randrange(6,9))
  103. num.append(nums)
  104. i=0
  105. for i in range(9):
  106. self.rows[i]=["x", "x", "x", "x", "x", "x", "x", "x", "x"]
  107. for number in num[i]:
  108. self.rows[i][number]=self.solution[i][number]
  109. def check_entries(self, row, column, entry):
  110. if self.rows[row][column]!="x":
  111. print "Sorry, this square has already been filled."
  112. self.turns-=1
  113. elif self.solution[row][column]==entry:
  114. print "\nCorrect."
  115. time.sleep(2)
  116. self.rows[row][column]=entry
  117. else:
  118. print "\nNo. Bad entry. Correct entry is being inserted."
  119. self.rows[row][column]=self.solution[row][column]
  120. self.wrong+=1
  121. time.sleep(2)
  122. def do_turn(self):
  123. self.print_me()
  124. self.turns+=1
  125. print "\nWrong guesses: "+str(self.wrong)+"\tTurns: "+str(self.turns)
  126. time.sleep(3)
  127. raw_input("Press ENTER when you are ready to continue")
  128. print "Input the row number of the block you wish to guess."
  129. row=get_integer(1, 10)
  130. print "Input the column number."
  131. column=get_integer(1, 10)
  132. print "Now what number do you think should be in this block?"
  133. change=get_integer(1, 10)
  134. print "\nNow, are you absolutely sure about these entries?"
  135. print "1. Yes"
  136. print "2. No"
  137. sure=raw_input()
  138. if sure =="1":
  139. self.check_entries(row-1, column-1, change)
  140. else:
  141. self.turns-=1
  142. print "Okay, think about it."
  143. time.sleep(2)
  144. def check(self):
  145. if self.wrong>=5:
  146. print "Sorry! Too many bad moves!\n\n\n"
  147. time.sleep(1)
  148. print""" GAME OVER - YOU LOST"""
  149. time.sleep(1)
  150. print """ reverting to main screen"""
  151. return False
  152. elif self.rows==self.solution:
  153. print "You solved it! You actually solved it."
  154. print "You conquered this puzzle in", self.turns, "turns, and only", wrong,"bad moves."
  155. time.sleep(1)
  156. print "\n\n\n"
  157. print """ GAME OVER - YOU WON"""
  158. time.sleep(1)
  159. print""" reverting to main screen"""
  160. return False
  161. else:
  162. return True
  163. def interface():
  164. print "\n\n\t\t9 Puzzle Sudoku\n"
  165. print "What grid qould you like?"
  166. print "1. 4x4"
  167. print "2. 6x6"
  168. print "3. 9x9"
  169. print "\n\n4. No Grid. Let me out"
  170. option=raw_input()
  171. if option=="1" or option=="2":
  172. print "sorry, this function has not yet been coded."
  173. if option=="4":
  174. print "\t\t\tGOODBYE"
  175. time.sleep(3)
  176. sys.exit()
  177. if option=="3":
  178. print "\nCreating your puzzle, please wait."
  179. grid=Grid(9)
  180. run=1
  181. while run:
  182. grid.do_turn()
  183. run=grid.check()
  184. interface()
  185. def get_integer(start, stop):
  186. try:
  187. x=int(raw_input())
  188. if x in range(start, stop):
  189. return x
  190. else:
  191. y=int(asdsd)
  192. except:
  193. print "You must enter a number between", start,"and",stop-1
  194. return get_integer(start, stop)
  195. interface()
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,521
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Projects for the Beginner

 
0
  #114
Apr 5th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: pyapplico is an unknown quantity at this point 
Solved Threads: 0
pyapplico pyapplico is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #115
Apr 7th, 2007
Create a webbrowser in 100 % Python.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is online now Online
Posting Virtuoso

Re: Projects for the Beginner

 
0
  #116
Apr 7th, 2007
Just an exercise in drawing letters with the wxPython GUI toolkit:
  1. # a nifty 'Hello' using wxPython drawing stuff
  2. # (you can add 'World' as a coding exercise)
  3.  
  4. import wx
  5.  
  6. class MyFrame(wx.Frame):
  7. """a frame with a panel"""
  8. def __init__(self, parent=None, id=-1, title=None):
  9. wx.Frame.__init__(self, parent, id, title)
  10. self.panel = wx.Panel(self, size=(500, 200))
  11. self.panel.Bind(wx.EVT_PAINT, self.on_paint)
  12. self.Fit()
  13.  
  14. def on_paint(self, event):
  15. # establish the painting surface of given color
  16. dc = wx.PaintDC(self.panel)
  17. bg_color = 'yellow'
  18. dc.SetBrush(wx.Brush(bg_color))
  19. dc.DrawRectangleRect((0, 0, 500, 200))
  20.  
  21. # draw 5 adjoining red circles with red fill
  22. dc.SetPen(wx.Pen('red', 1))
  23. dc.SetBrush(wx.Brush('red'))
  24. y = 100
  25. r = 50
  26. for k in range(1, 6):
  27. x = k * 100 - 50
  28. dc.DrawCircle(x, y, r)
  29.  
  30. # cut out a few notches for the letters
  31. # draw background colored rectangles (x, y, w, h)
  32. dc.SetPen(wx.Pen(bg_color, 1))
  33. dc.SetBrush(wx.Brush(bg_color))
  34. dc.DrawRectangleRect((40, 50, 20, 40))
  35. dc.DrawRectangleRect((40, 110, 20, 40))
  36. dc.DrawRectangleRect((150, 70, 50, 20))
  37. dc.DrawRectangleRect((150, 110, 50, 20))
  38. dc.DrawRectangleRect((240, 50, 60, 70))
  39. dc.DrawRectangleRect((340, 50, 60, 70))
  40. dc.DrawCircle(450, 100, 20)
  41.  
  42.  
  43. app = wx.PySimpleApp()
  44. frame1 = MyFrame(title='the hello circles')
  45. frame1.Center()
  46. frame1.Show()
  47. app.MainLoop()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1
Reputation: rezasalar is an unknown quantity at this point 
Solved Threads: 0
rezasalar rezasalar is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
-1
  #117
May 3rd, 2007
hi
I am new member
i am starting python since 2 weeks ago ,and want to learn it from here .
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,273
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 175
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Projects for the Beginner

 
0
  #118
May 7th, 2007
Predict the outcome of this Python code:
  1. def sub_one(x):
  2. print "x before if -->", x
  3. if x > 1:
  4. x = x - 1
  5. sub_one(x)
  6. print "x after if -->", x
  7. return x
  8.  
  9. x = 5
  10. final = sub_one(x)
  11. print "final result -->", final
Can you explain the odd behaviour?
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 12
Reputation: StrikerX is an unknown quantity at this point 
Solved Threads: 0
StrikerX StrikerX is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #119
May 19th, 2007
Module for handling strings (learning stuff)
  1. #######################################
  2. # Programmed by : StrikerX
  3. # Purpose : Module for handling string
  4. #######################################
  5.  
  6.  
  7. def toUpper(string):
  8. string_upper = ""
  9. for char in string :
  10. if ord(char) < 97 :
  11. string_upper += char
  12. elif ord(char) >= 97 :
  13. string_upper += chr(ord(char) - 32)
  14. print string_upper
  15.  
  16.  
  17.  
  18. def toLower(string):
  19. string_Lower = ""
  20. for char in string :
  21. if ord(char) > 97 :
  22. string_Lower += char
  23. elif ord(char) <= 97:
  24. string_Lower += chr(ord(char) + 32 )
  25. print string_Lower
  26.  
  27.  
  28. def swapCase(string):
  29. string_Swapped = ""
  30. for char in string :
  31. if ord(char) >= 97:
  32. string_Swapped += chr(ord(char) - 32 )
  33. elif ord(char) < 97:
  34. string_Swapped += chr(ord(char) + 32 )
  35. print string_Swapped
  36.  
  37.  
  38. def lenString(string):
  39. count = 0
  40. for char in string :
  41. count += 1
  42. return count
  43.  
  44.  
  45. def exist(char, string):
  46. if char in String :
  47. print "0" #True
  48. else :
  49. print "1" #False
  50.  
  51.  
  52.  
  53.  
  54. def startsWith(char, string):
  55. if string[0] == char :
  56. print "0" #True
  57. else :
  58. print "1" #False
  59.  
  60.  
  61. def endsWith(char, string):
  62. if string[len(string) - 1] == char:
  63. print "0" #True
  64. else :
  65. print "1" #False
  66.  
  67. def index(string):
  68. print string[0]
  69.  
  70.  
  71. def capitalize(string):
  72. Str = ""
  73. char = ''
  74. x = string[0]
  75. if ord(string[0]) < 97 :
  76. char = string[0]
  77. if ord(string[0]) >= 97 :
  78. char = chr(ord(string[0]) - 32)
  79. Str = char + string[1:]
  80. print Str
  81.  
  82.  
  83.  
  84.  
  85. def printf(string):
  86. print string
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 12
Reputation: StrikerX is an unknown quantity at this point 
Solved Threads: 0
StrikerX StrikerX is offline Offline
Newbie Poster

Re: Projects for the Beginner

 
0
  #120
May 19th, 2007
  1. #!bin/python
  2.  
  3. #-###############################
  4. # Programmer : StrikerX
  5. # Purpose : Cat Tool
  6. ##################################
  7.  
  8. #cat.py state fileName
  9. #cat.py -h
  10.  
  11. import sys
  12. import os
  13.  
  14.  
  15. def read():
  16. fileName = sys.argv[2]
  17. f = open(fileName, "r")
  18. for line in f.readlines():
  19. print line
  20. f.close()
  21.  
  22.  
  23. def append():
  24. arr = []
  25. fileName = sys.argv[2]
  26. if os.path.exists(fileName):
  27. f = open(fileName, "a")
  28. buf = "string"
  29. while buf != "":
  30. buf = raw_input("")
  31. arr.append(buf)
  32. for x in arr:
  33. f.write(x + '\n')
  34. f.close()
  35.  
  36.  
  37.  
  38. def write():
  39. fileName = sys.argv[2]
  40. arr = []
  41. f = open(fileName, "w")
  42. buf = "string"
  43. while buf != "":
  44. buf = raw_input("")
  45. arr.append(buf)
  46. for x in arr:
  47. f.write(x + '\n')
  48. f.close()
  49.  
  50. def h():
  51. print "\tParam -> State"
  52. print "\n\t > -> Write\n\t>> -> Append\n\t-r -> read\n\t-a -> About\n\t-h -> Help Menu"
  53.  
  54.  
  55.  
  56.  
  57. param = sys.argv[1]
  58.  
  59. if param == "-h":
  60. fileName = ""
  61. h()
  62. elif param == "-r":
  63. read()
  64. elif param == "-w":
  65. write()
  66. elif param == "-a":
  67. append()
  68. else:
  69. print "an illegal parameter !!"
Last edited by StrikerX; May 19th, 2007 at 10:10 pm.
Reply With Quote Quick reply to this message  
Reply

Tags
beginner, projects, python

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC