This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-11-16 15:58:01

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

My Python Project

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

 

#2 2012-11-16 16:34:03

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: My Python Project

Mediafire or something, I think.


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#3 2012-11-16 16:43:22

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: My Python Project

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?


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#4 2012-11-16 17:02:26

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: My Python Project

It's just a script, right, so you could put it in a [ code ] tag and post it here.


Fork Clamor on GitHub!

Offline

 

#5 2012-11-16 21:43:59

technoguyx
Scratcher
Registered: 2008-10-18
Posts: 1000+

Re: My Python Project

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.  smile


http://getgnulinux.org/links/en/linuxliberated_4_78x116.png

Offline

 

#6 2012-11-17 02:05:54

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

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

while 1:
    clock ()

Thanks for all of your help!

Last edited by JH1010 (2012-11-17 10:27:49)

Offline

 

#7 2012-11-17 07:39:15

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

Belongs to MISC


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#8 2012-11-17 09:36:17

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

fanofcena wrote:

Belongs to MISC

Oh... Sorry. I thought it would belong here since it is still about programming.

Offline

 

#9 2012-11-17 09:41:24

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

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.  hmm


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#10 2012-11-17 10:15:11

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: My Python Project

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.  smile

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 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:

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 ()

smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#11 2012-11-17 10:26:20

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

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.  smile

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 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:

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 ()

smile

Thanks! I will edit the code post and the file.

Last edited by JH1010 (2012-11-17 10:26:51)

Offline

 

#12 2012-11-17 10:32:22

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

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.  smile

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 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:

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 ()

smile

Hahaha good catch ^_^


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#13 2012-11-17 11:42:58

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

Alternatively you can use threads with the thread module.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#14 2012-11-17 13:01:55

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

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 ?


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#15 2012-11-17 13:06:06

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

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

 

#16 2012-11-17 14:23:59

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

Alternatively you can use threads with the thread module.

What would you use threads for here?  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#17 2012-11-17 17:08:02

ohaiderstudios
Scratcher
Registered: 2010-10-31
Posts: 100+

Re: My Python Project

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.


Fork Clamor on GitHub!

Offline

 

#18 2012-11-17 19:55:11

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

blob8108 wrote:

Hardmath123 wrote:

Alternatively you can use threads with the thread module.

What would you use threads for here?  smile

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.


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#19 2012-11-18 00:13:04

fanofcena
Scratcher
Registered: 2008-07-03
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

blob8108 wrote:

Hardmath123 wrote:

Alternatively you can use threads with the thread module.

What would you use threads for here?  smile

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)


http://i53.tinypic.com/2vxr2c0.png Click whats above u might make a cute planet happy ^_^

Offline

 

#20 2012-11-18 02:26:06

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

fanofcena wrote:

Hardmath123 wrote:

blob8108 wrote:


What would you use threads for here?  smile

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.  tongue


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#21 2012-11-18 02:57:03

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: My Python Project

fanofcena wrote:

Hardmath123 wrote:

blob8108 wrote:


What would you use threads for here?  smile

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

 

#22 2012-11-18 04:23:55

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

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.  hmm


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

#23 2012-11-18 07:44:05

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: My Python Project

Hardmath123 wrote:

blob8108 wrote:

Hardmath123 wrote:

Alternatively you can use threads with the thread module.

What would you use threads for here?  smile

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  tongue  Why does the clock function want to call itself at all? Surely a while loop is what you want...


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#24 2012-11-18 08:14:43

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: My Python Project

blob8108 wrote:

Hardmath123 wrote:

blob8108 wrote:


What would you use threads for here?  smile

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  tongue  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

 

#25 2012-11-18 08:23:49

Hardmath123
Scratcher
Registered: 2010-02-19
Posts: 1000+

Re: My Python Project

blob8108 wrote:

Hardmath123 wrote:

blob8108 wrote:

What would you use threads for here?  smile

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  tongue  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:

Code:

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:

Code:

function loop() {
// ...
window.setTimeout(loop,1);
}

This way you keep going up and running the loop every millisecond. Get it?  smile

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)


Hardmaths-MacBook-Pro:~ Hardmath$ sudo make $(whoami) a sandwich

Offline

 

Board footer