What is wrong in this example code?
fl
rxjwg98 at gmail.com
Thu Nov 12 09:07:45 EST 2015
On Thursday, November 12, 2015 at 8:58:33 AM UTC-5, fl wrote:
> Hi,
>
> I run a code snippet from link:
> http://www.python-course.eu/inheritance_example.php
>
> It is found that there is an error in this loop:
>
> for i in xrange(10000):
> x.tick()
> print(x)
> SyntaxError: invalid syntax
>
>
> I have modified it to:
> for i in x range(10000):
> x.tick()
> print(x)
> SyntaxError: invalid syntax
>
> it still has an error. What could be wrong?
>
> Thanks,
>
>
> ....
> class Clock(object):
>
> def __init__(self,hours=0, minutes=0, seconds=0):
> self.__hours = hours
> self.__minutes = minutes
> self.__seconds = seconds
>
> def set(self,hours, minutes, seconds=0):
> self.__hours = hours
> self.__minutes = minutes
> self.__seconds = seconds
>
> def tick(self):
> """ Time will be advanced by one second """
> if self.__seconds == 59:
> self.__seconds = 0
> if (self.__minutes == 59):
> self.__minutes = 0
> self.__hours = 0 if self.__hours==23 else self.__hours+1
> else:
> self.__minutes += 1;
> else:
> self.__seconds += 1;
>
> def display(self):
> print("%d:%d:%d" % (self.__hours, self.__minutes, self.__seconds))
>
> def __str__(self):
> return "%2d:%2d:%2d" % (self.__hours, self.__minutes, self.__seconds)
>
> x = Clock()
> print(x)
> for i in xrange(10000):
> x.tick()
> print(x)
Solved it by this:
print(x)
for i in range(1, 10000):
x.tick()
print(x)
Thanks,
More information about the Python-list
mailing list