time.localtime() question beginner python
I'm having a syntax error on the second line of this code, I'm trying to make a counter with the winsound beep.
I think t开发者_运维技巧he problem is with the format() part, but i get a highlighted =, equals sign when i try to run the program. syntax error
def print_time(secs):
print('{0}:{1:02}'.format(secs//60,secs%60),end=' ')
print("left to wait...")
This is my second week programming, very basic understanding of comp sci or of languages.
This looks like a wonderful site to learn from.
If the part of the code I wrote looks fine, i can post the rest of it up as well to help find the problem.
It sounds like you're reading documentation for Python 3.x, but running Python 2.x. Try this instead:
def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
Also, divmod()
.
def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
The above code should work fine.
Python 3+ treats 'print' as a function and hence introduces end=' ' to suppress newline. But, in earlier versions of python it was done by appending ,(comma) to the print statement. See this link for what's new in Python 3+.
Apparently, your Python environment is 2.x and hence you are seeing the error.
精彩评论