I'm pretty much a complete beginner. I'm trying to get a 'timer' or 'clock' that pretty much just counts how long you have the window open by using a loop that adds 1 to "a" for every second, and adds 1 to "b" for every 60 seconds (while subtracting 60 from "a" to reset it to 0).

import time

a = 0
b = 0
FirstRun = True

while 1 == 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
    print("The current time is" + b + ":" + a)
    time.sleep(1)

However, when I try to run it, I get this error:

line 12, in <module>
print("The current time is" + b + ":" + a)
TypeError: Can't convert 'int' object to str implicitly

I have no idea how to fix this. Any suggestions?

(Also, I'm using 3.1.2)

Recommended Answers

All 19 Replies

try this

import time,

a = 0
b = 0
FirstRun = True

while 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
       
    print("The current time is  %d :%d"%( b  ,a))
    time.sleep(1)

;)

look you can add this to make it a little interesting

import time



a = 0
b = 0
FirstRun = True

while 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
    print '\n' *100   
    print("The current time is  %d :%d"%( b  ,a))
    time.sleep(1)

This makes the screen clear so that it looks like a real time.
;)

Thanks! This was a lot of help!

Another easier way to do that would be to simply make b and a strings. This is also useful for many other things, and is slightly easier to understand.

import time



a = 0
b = 0
FirstRun = True

while 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
    print '\n' *100   
    print "The current time is", str(b) + ":" + str(a)
    time.sleep(1)

(e.g. str(n) uses n as a string)
Cheers!

Well mark the thread close and if you wish to upvote me....... you are welcome. ;)

Well mark the thread close and if you wish to upvote me....... you are welcome. ;)

Actually your "real time" upgrade will probably take so long to be processed (100 new lines isn't instantaneous) that the clock will be inaccurate :)

aaahhh do you have any benchmark to your claims.???

print('\n' *100)
is use for clearing the screen if you do not know.
it is just like os.system('clear') on *nix system and os.system('CLS') on dos,windows.

print "\n"*100 works for cross platform. ok ??
;)

That sounded official and scary :)
Eh, there's nothing "cross platform" blah about it: its very basic Python syntax.... It just prints newline ("\n") 100 times. This is slow. Check it out with the timeit module....

VulcanDesign.....

Can you please give a benchmark to support your claim or just dont write what you dont know.
The world is waiting for your benchmark pleeeeeeeeeeeeeease

;)

Besides the screen clearing prove i have given you... and besides the one i say it works cross platform...

Do you have a different one?
If not. Please dont disturb anyone anymore ok?
;)

Note. the syntax is not print("\n") *100 , but print("\n" *100) and this prints nothing ok??? Blah

Its python syntatic suger. have a try ok?

try this

import time,

a = 0
b = 0
FirstRun = True

while 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
       
    print("The current time is  %d :%d"%( b  ,a))
    time.sleep(1)

;)

How about

from __future__ import print_function
import time

a = 0
b = 0
FirstRun = True
old_t =''
print("The current time is: ", end='')
while 1 == 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
    t = "%02i:%02i" % (b,a)
    print('\b'*len(old_t)+t, end='')
    old_t = t
    
    time.sleep(1)

Does not work from IDLE, though, only when run directly.

How about

from __future__ import print_function
import time

a = 0
b = 0
FirstRun = True
old_t =''
print("The current time is: ", end='')
while 1 == 1:               
    a = a + 1               #Adds one second
    if a > 59:              #Accounts for minutes
        b = b + 1
        a = a - 60
    t = "%02i:%02i" % (b,a)
    print('\b'*len(old_t)+t, end='')
    old_t = t
    
    time.sleep(1)

Does not work from IDLE, though, only when run directly.

Of course if you would like to show real time instead of doing exercise you would do for example:

from __future__ import print_function
import time

old_t =''
print("The current date and time are: ", end='')
while True:
    t = time.asctime()
    print('\b'*len(old_t)+t, end='')
    old_t = t

    time.sleep(1)

Of course if you would like to show real time instead of doing exercise you would do for example:
Python Syntax (Toggle Plain Text)

1.
from __future__ import print_function
2.
import time
3.

4.
old_t =''
5.
print("The current date and time are: ", end='')
6.
while True:
7.
t = time.asctime()
8.
print('\b'*len(old_t)+t, end='')
9.
old_t = t
10.

11.
time.sleep(1)

Well Ysisoft....

I have added some nice bits to your code. I think we are getting there.

Check this out.

from __future__ import print_function
import time,os

old_t =''
while True:
    t = time.asctime()
    print((os.system('clear'), "The current date and time are: ",u''*len(old_t)+t, end='')
    old_t = t
    time.sleep(1)

I do not think it is so system independant to use clear, therefore I use backspacing to remove minimum amount of text without destroying other text on screen:

rom __future__ import print_function
import time
wipe = len(time.asctime())*'\b'
print("The current date and time are: "+' '*len(wipe),end='')
while True:
    print(wipe+time.asctime(), end='')
    time.sleep(1)

Ysisoft..

Have you tried your code?

The logic on line 3 ie. wipe=len(time.asctime())*'\b'

Instead of backspacing... It is forwardspacing time the len.
That is on idle.
On Geany it is not outputting anything.
And on my favourite Pydev kum Aptana/c++/Java/php It is doing the same like Idle with unicode error

I've got it clocking in at .005 of a second (The 100 new lines). :)

The code does work by double clicking. Not with Python 3 though. Reason I do not know. To do clear screen more general way is chr(12) ie ctrl-L, but that does not function in Windows, where you need CLS. Back space does not seem to like Python 3.

Here is corrected version with advice from StackOverflow:

from __future__ import print_function
import time
import sys
wipe = '\b'*len(time.asctime())
print("The current date and time are: "+' '*len(wipe), end='')
while True:
    print(wipe+time.asctime(), end='')
    sys.stdout.flush()
    time.sleep(1)

stdout must be flushed manually if not use newline.

I recommend of trying to use ContextEditor and setup starting the python program by F9-, commands. You can easily test multiple versions of Python, and program does not run in changed environment.

Here is corrected version with advice from StackOverflow:
Python Syntax (Toggle Plain Text)

1.
from __future__ import print_function
2.
import time
3.
import sys
4.
wipe = '\b'*len(time.asctime())
5.
print("The current date and time are: "+' '*len(wipe), end='')
6.
while True:
7.
print(wipe+time.asctime(), end='')
8.
sys.stdout.flush()
9.
time.sleep(1)

from __future__ import print_function import time import sys wipe = '\b'*len(time.asctime()) print("The current date and time are: "+' '*len(wipe), end='') while True: print(wipe+time.asctime(), end='') sys.stdout.flush() time.sleep(1)

stdout must be flushed manually if not use newline.

I recommend of trying to use ContextEditor and setup starting the python program by F9-, commands. You can easily test multiple versions of Python, and program does not run in changed environment.
23 Hours Ago 1:23 pm

tonyjv

There is something wrong with all these IDEs. All the known ide's for python failed to execute your code well. Syntaticaly your code it right so i could not think of any errors again. You are matured with python than i am.

Well it only worked flawlessly via CMD prompt.....
The question is... What is wrong?? the ide's or pyhton????
coming from C++ to python, i find a lot of stuffs scary at times with python. Today(Nov 28 ) is exactly 17 days since i started pythoning.

Yea i know. Python is easy to pick up.
Well good one there.
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.