Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 3rd, 2005, 11:56 PM   #1
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Alright, I got bored and needed a timer to time my speechs for school, so I made one. Here is that. The only drawback is that it needs winsound to work, but if you're not on windows and the beep isnt that important, just comment anything out about beeping.

from time import sleep 
from winsound import Beep # Can be commented out

def timer(sec):
  x = 0
  while x < sec + 1:
    if x == sec:
      print x
      Beep(2000,500) # Can be commented out
      Beep(2000,500)# Can be commented out
      Beep(2000,1000)# Can be commented out
      break
    else:
      print x
      x = x + 1
      sleep(1)

The next calculates Pythagorean Triples, and I made it to get some bonus points in geometry. In my 80's math book, they show how to do it in basic, and it only shows one, so I decided to beef the program up a lot on my own. Here it is.
# Usage: When ever this file is opened (python file.py) to run, you can call 
# findTripple(number) to run. number needs to be replaced with a number of
# what number you want to find the tripplets to from 0.
# If you only want to find one set of tripplets for a certain nuber, then you can 
# just call pythag(number) again number being number you want to find the tripplet
# for and the only number, none before it.


def save(a,b,c,n): # This is called to write the numbers to a file.
  a = str(a) # These next four conver the ints to stings so they can be written.
  b = str(b)
  c = str(c)
  n = str(n)
  file = open("tripple.txt","a")
  file.write("n = ")
  file.write(n)
  file.write("\t")
  file.write(a +" ")
  file.write(b + " ")
  file.write(c + " ")
  file.write("\n")

def pythag(n): # This little method does the actual calculations.
  a = 2*n + 1
  b = 2*(n**2) + 2*n
  c = 2*(n**2) + 2*n + 1
  print "n =",n,":",a, b, c,"(",a**2,b**2,c**2,"=",a**2+b**2,")"
  save(a,b,c,n)

def findTripple(t): # This is what needs to be called when the file is opened.
  x = 0
  while x < t + 1:
    pythag(x)
    x = x + 1

These were both made basicly for my own purpous, but you never know if someone else can use it. Plus I encourange scrutiny of my code since I have no classes to take and this is the only way to learn.

Anyway, enjoy

Edit:
Also in case you were wondering, here is the orignal basic program that I got the idea off of.
10 FOR X = 2 TO 7
20  FOR Y = 1 TO X - 1
30   LET A = 2 * X * Y
40   LET B = X * X - Y * Y
50   LET C = X * X + Y * Y
60   PRINT A;",";B;",";C
70  NEXT Y
80 NEXT X
90 END
thechristelegacy is offline   Reply With Quote
Old Jan 4th, 2005, 4:24 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,475
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Everyone needs a good timer... I also wrote code for mathematical problems / puzzles... but it was in C++. Good work man.
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Jan 17th, 2005, 3:09 PM   #3
Xero
Hobbyist Programmer
 
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 5 Xero is on a distinguished road
nice

haha i remember the beeps. Senior year of high school during class, we would make songs during the class with the different beeps. It was great.
Xero is offline   Reply With Quote
Old Jan 17th, 2005, 5:30 PM   #4
thechristelegacy
Expert Programmer
 
thechristelegacy's Avatar
 
Join Date: Jul 2004
Location: Somerset, Pa
Posts: 708
Rep Power: 5 thechristelegacy is on a distinguished road
Send a message via AIM to thechristelegacy Send a message via MSN to thechristelegacy
Yeah, I wrote mary had a little lamb once.
thechristelegacy is offline   Reply With Quote
Old Jan 17th, 2005, 5:37 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I can play Mary had a Little Lamb using my mobile phone keys

3 2 1 2 3 3 3- 2 2 2- 3 3 3- 3 2 1 2 3 3 3 1 2 2 3 2 1--
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:58 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC