I have made a stopwatch in Python. I would like to share this in the community. Is there any way to upload it to the internet?
Offline
There are loads of places you can upload; there's no way of running Python code as a browser plugin or anything like that, that I know of (it'd be pretty hard to make secure). What kind of thing were you thinking of?
Offline
It's just a script, right, so you could put it in a [ code ] tag and post it here.
Offline
For a short script, copy/pasting it into [code] tags might be fine.
You can also put simple modules in Pastebin. It has syntax highlight (which is pretty neat and really helps understand code) for most popular languages including Python. c:
For larger projects that involve multiple modules, external resources (images, music) and the such, you can get a GitHub account and create a repository for your project there. It's free if your project is open source.
Offline
The script is short-ish (about 25 lines) I'll put it in code tags.
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 while 1: clock ()
Thanks for all of your help!
Last edited by JH1010 (2012-11-17 10:27:49)
Offline
fanofcena wrote:
Belongs to MISC
Oh... Sorry. I thought it would belong here since it is still about programming.
Offline
I think it's fine here, because the people over at misc aren't really programmers, and all the coding topics I posted there get buried pretty quick.
Offline
JH1010 wrote:
The script is short-ish (about 25 lines) I'll put it in code tags.
Code:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 clock () clock ()Thanks for all of your help!
Just to let you know: your current code will crash after 16 minutes and 37 seconds.
This is because you're calling the function recursively: you're calling clock() from inside the clock function. You've created an infinite recursion. By removing the "time.sleep(1)" line, you can see the error:
... File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 12, in clock print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") RuntimeError: maximum recursion depth exceeded while calling a Python object
What you want to do instead is call the function repeatedly. You can use "while 1" (or "while True") to do this, which behaves like Scratch's "forever" block:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 while 1: clock ()
Offline
blob8108 wrote:
JH1010 wrote:
The script is short-ish (about 25 lines) I'll put it in code tags.
Code:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 clock () clock ()Thanks for all of your help!
Just to let you know: your current code will crash after 16 minutes and 37 seconds.
This is because you're calling the function recursively: you're calling clock() from inside the clock function. You've created an infinite recursion. By removing the "time.sleep(1)" line, you can see the error:Code:
... File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 12, in clock print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") RuntimeError: maximum recursion depth exceeded while calling a Python objectWhat you want to do instead is call the function repeatedly. You can use "while 1" (or "while True") to do this, which behaves like Scratch's "forever" block:
Code:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 while 1: clock ()
Thanks! I will edit the code post and the file.
Last edited by JH1010 (2012-11-17 10:26:51)
Offline
blob8108 wrote:
JH1010 wrote:
The script is short-ish (about 25 lines) I'll put it in code tags.
Code:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 clock () clock ()Thanks for all of your help!
Just to let you know: your current code will crash after 16 minutes and 37 seconds.
This is because you're calling the function recursively: you're calling clock() from inside the clock function. You've created an infinite recursion. By removing the "time.sleep(1)" line, you can see the error:Code:
... File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 24, in clock clock () File "clock.py", line 12, in clock print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") RuntimeError: maximum recursion depth exceeded while calling a Python objectWhat you want to do instead is call the function repeatedly. You can use "while 1" (or "while True") to do this, which behaves like Scratch's "forever" block:
Code:
import time hours = 0 minutes = 0 seconds = 0 def clock (): global hours global minutes global seconds print (int(hours),"h:",int(minutes),"m:",int(seconds),"s") time.sleep (1) seconds = seconds + 1 if seconds > 59: minutes = minutes + 1 seconds = 0 if minutes > 59: hours = hours + 1 minutes = 0 while 1: clock ()
Hahaha good catch ^_^
Offline
Alternatively you can use threads with the thread module.
Offline
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
he barely knows how to ride a tri-cycle and you are suggesting him to drive an M1-Ebriams ?
Offline
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
I don't know what that means.
fanofcena wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
He barely knows how to ride a tri-cycle and you are suggesting him to drive an M1-Ebriams?
Is that a reference to my Python knowledge and what Hardmath123 is asking me to do? If it is then you're right.
Offline
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
What would you use threads for here?
Offline
fanofcena wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
he barely knows how to ride a tri-cycle and you are suggesting him to drive an M1-Ebriams ?
Well put.
Offline
blob8108 wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
What would you use threads for here?
Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
Another way to do it would be to write a generator which yields the current time after each second, but I think that's overkill.
Offline
Hardmath123 wrote:
blob8108 wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
What would you use threads for here?
Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
Another way to do it would be to write a generator which yields the current time after each second, but I think that's overkill.
wrong it will cause a recursion error because threads will have a call stack aswell. try that in javascript ;D or even better try that in node.js forking child_process [ which runs on another thread ] or if u want u can try doing that with a C++ module linked with node.js that runs in completely different thread :-) and hey JavaScript is a different methodology its a single-threaded-event-loop necessarily so a forever loop in that will obviously be a mess for browser because it has to do rendering after the JS events finish including CSS and HTML parsing which makes browser wait for infinite time..
Now if u can actually run it on node.js a while(true){
} loop is not a problem for it because nothing is waiting for it [ provided its just like a simple clock program ]
JH1010 wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
I don't know what that means.
fanofcena wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
He barely knows how to ride a tri-cycle and you are suggesting him to drive an M1-Ebriams?
Is that a reference to my Python knowledge and what Hardmath123 is asking me to do? If it is then you're right.
yes.
Last edited by fanofcena (2012-11-18 00:15:14)
Offline
fanofcena wrote:
Hardmath123 wrote:
blob8108 wrote:
What would you use threads for here?Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
Another way to do it would be to write a generator which yields the current time after each second, but I think that's overkill.wrong it will cause a recursion error because threads will have a call stack aswell. try that in javascript ;D or even better try that in node.js forking child_process [ which runs on another thread ] or if u want u can try doing that with a C++ module linked with node.js that runs in completely different thread :-) and hey JavaScript is a different methodology its a single-threaded-event-loop necessarily so a forever loop in that will obviously be a mess for browser because it has to do rendering after the JS events finish including CSS and HTML parsing which makes browser wait for infinite time..
Now if u can actually run it on node.js a while(true){
} loop is not a problem for it because nothing is waiting for it [ provided its just like a simple clock program ]
I'll try to understand that once someone puts some periods and commas in.
Offline
fanofcena wrote:
Hardmath123 wrote:
blob8108 wrote:
What would you use threads for here?Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
Another way to do it would be to write a generator which yields the current time after each second, but I think that's overkill.wrong it will cause a recursion error because threads will have a call stack aswell. try that in javascript ;D or even better try that in node.js forking child_process [ which runs on another thread ] or if u want u can try doing that with a C++ module linked with node.js that runs in completely different thread :-) and hey JavaScript is a different methodology its a single-threaded-event-loop necessarily so a forever loop in that will obviously be a mess for browser because it has to do rendering after the JS events finish including CSS and HTML parsing which makes browser wait for infinite time..
Now if u can actually run it on node.js a while(true){
} loop is not a problem for it because nothing is waiting for it [ provided its just like a simple clock program ]
Am I meant to be able to understand that?
Offline
JH1010 wrote:
fanofcena wrote:
Hardmath123 wrote:
Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
Another way to do it would be to write a generator which yields the current time after each second, but I think that's overkill.wrong it will cause a recursion error because threads will have a call stack aswell. try that in javascript ;D or even better try that in node.js forking child_process [ which runs on another thread ] or if u want u can try doing that with a C++ module linked with node.js that runs in completely different thread :-) and hey JavaScript is a different methodology its a single-threaded-event-loop necessarily so a forever loop in that will obviously be a mess for browser because it has to do rendering after the JS events finish including CSS and HTML parsing which makes browser wait for infinite time..
Now if u can actually run it on node.js a while(true){
} loop is not a problem for it because nothing is waiting for it [ provided its just like a simple clock program ]Am I meant to be able to understand that?
Maybe if someone adds a couple of periods and commas.[/sarcasm]
It was meant for me, but I really don't grok.
Offline
Hardmath123 wrote:
blob8108 wrote:
Hardmath123 wrote:
Alternatively you can use threads with the thread module.
What would you use threads for here?
Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
I don't understand that Why does the clock function want to call itself at all? Surely a while loop is what you want...
Offline
blob8108 wrote:
Hardmath123 wrote:
blob8108 wrote:
What would you use threads for here?Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
I don't understand that Why does the clock function want to call itself at all? Surely a while loop is what you want...
Well, there are loads of ways to make a loop. One of them is using a recursive function.
Offline
blob8108 wrote:
Hardmath123 wrote:
blob8108 wrote:
What would you use threads for here?
Well, ideally I would start a new thread from the clock function, which (AFAIK) would not cause the recursion error. I think that's nicer, because functions that go into infinite loops never really appealed to me, probably because of my JS background where you'll freeze the browser.
I don't understand that Why does the clock function want to call itself at all? Surely a while loop is what you want...
I didn't get this either in the beginning, but here's the deal: in JS, the browser tries to run all code until it's over, and if it takes long you get the spinning colorful wheel (of death). Try running this:
while (true) { }
It freezes.
So what you want to do in JS is to set up a timer which starts a new thread after a slight delay:
function loop() { // ... window.setTimeout(loop,1); }
This way you keep going up and running the loop every millisecond. Get it?
EDIT: Of course, the modern newfangled way to do it is to use RequestAnimationFrame() which basically asks the browser politely to animate it while the browser gets to be stingy with resources (including pausing/slowing down animations when the tab is blurred).
Last edited by Hardmath123 (2012-11-18 08:34:23)
Offline