![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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 + 1These 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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
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." |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Dec 2004
Location: a cardboard box
Posts: 118
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Yeah, I wrote mary had a little lamb once.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|