Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98700
| From | John Wong <gokoproject@gmail.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: What is wrong in this example code? |
| Date | 2015-11-12 10:10 -0500 |
| Message-ID | <mailman.273.1447341014.16136.python-list@python.org> (permalink) |
| References | <1b204966-46c6-4890-a5a5-8addd20343a1@googlegroups.com> <383401eb-f8be-4f7a-bd63-3520eedc069e@googlegroups.com> |
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@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
>
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
What is wrong in this example code? fl <rxjwg98@gmail.com> - 2015-11-12 05:57 -0800
Re: What is wrong in this example code? fl <rxjwg98@gmail.com> - 2015-11-12 06:07 -0800
Re: What is wrong in this example code? John Wong <gokoproject@gmail.com> - 2015-11-12 10:10 -0500
Re: What is wrong in this example code? Larry Hudson <orgnut@yahoo.com> - 2015-11-12 15:59 -0800
Re: What is wrong in this example code? Nathan Hilterbrand <nhilterbrand@gmail.com> - 2015-11-12 19:24 -0500
csiph-web