What is wrong in this example code?

John Wong gokoproject at gmail.com
Thu Nov 12 10:10:06 EST 2015


If you are using Python 3 you will need to change xrange to range, but the
error shouldn't be invalid syntax. I remember it should just name name not
found/not defined. So if you are not using Python 3, range and xrange do
still in Python 2 and they have different use case.

So i am really curious how you fixed it. It sounds more like some issue
with space rather, but still glad you solved it somehow.

On Thu, Nov 12, 2015 at 9:07 AM, fl <rxjwg98 at gmail.com> wrote:

> 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,
> --
> https://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list