Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #98693 > unrolled thread
| Started by | fl <rxjwg98@gmail.com> |
|---|---|
| First post | 2015-11-12 05:57 -0800 |
| Last post | 2015-11-12 19:24 -0500 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-12 05:57 -0800 |
| Subject | What is wrong in this example code? |
| Message-ID | <1b204966-46c6-4890-a5a5-8addd20343a1@googlegroups.com> |
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)
[toc] | [next] | [standalone]
| From | fl <rxjwg98@gmail.com> |
|---|---|
| Date | 2015-11-12 06:07 -0800 |
| Message-ID | <383401eb-f8be-4f7a-bd63-3520eedc069e@googlegroups.com> |
| In reply to | #98693 |
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,
[toc] | [prev] | [next] | [standalone]
| From | John Wong <gokoproject@gmail.com> |
|---|---|
| Date | 2015-11-12 10:10 -0500 |
| Message-ID | <mailman.273.1447341014.16136.python-list@python.org> |
| In reply to | #98695 |
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
>
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2015-11-12 15:59 -0800 |
| Message-ID | <BJednVGxdfl7utjLnZ2dnUU7-K-dnZ2d@giganews.com> |
| In reply to | #98695 |
On 11/12/2015 06:07 AM, fl wrote:
> On Thursday, November 12, 2015 at 8:58:33 AM UTC-5, fl wrote:
>> Hi,
>>
<snip>
>> 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;
<snip>
Nothing to do with your original question, just a trivial suggestion which you are free to
ignore. You can shorten this tick() method by using the divmod() function. It does a division
and returns both the quotient AND the remainder. IOW, given divmod(x, y) it returns the tuple
(x/y, x%y). It is a very useful function. Try this:
def tick(self):
xtra, self._seconds = divmod(self._seconds + 1, 60)
xtra, self._minutes = divmod(self._minutes + xtra, 60)
self._hours += xtra
Explanation: (to shorten this, I'm leaving off the leading "self._" from the actual code.)
The first divmod() sets xtra = (seconds+1) / 60, which is the 'overflow' if any, from the
division by 60, and seconds is the updated seconds limited to 0-59 (the result of (seconds+1) %
60). The second divmod() does the same thing to update minutes (if xtra != 0) and xtra is set
to the 'overflow' from that division, which is then added to hours.
More confusing perhaps, but definitely shorter.
As I said above, use it if you want or ignore it if it's too confusing.
-=- Larry -=-
[toc] | [prev] | [next] | [standalone]
| From | Nathan Hilterbrand <nhilterbrand@gmail.com> |
|---|---|
| Date | 2015-11-12 19:24 -0500 |
| Message-ID | <mailman.287.1447419693.16136.python-list@python.org> |
| In reply to | #98722 |
On Thu, Nov 12, 2015 at 6:59 PM, Larry Hudson via Python-list <
python-list@python.org> wrote:
Nothing to do with your original question, just a trivial suggestion which
> you are free to ignore. You can shorten this tick() method by using the
> divmod() function. It does a division and returns both the quotient AND
> the remainder. IOW, given divmod(x, y) it returns the tuple (x/y, x%y).
> It is a very useful function. Try this:
>
> def tick(self):
> xtra, self._seconds = divmod(self._seconds + 1, 60)
> xtra, self._minutes = divmod(self._minutes + xtra, 60)
> self._hours += xtra
>
> Explanation: (to shorten this, I'm leaving off the leading "self._" from
> the actual code.)
> The first divmod() sets xtra = (seconds+1) / 60, which is the 'overflow'
> if any, from the division by 60, and seconds is the updated seconds limited
> to 0-59 (the result of (seconds+1) % 60). The second divmod() does the
> same thing to update minutes (if xtra != 0) and xtra is set to the
> 'overflow' from that division, which is then added to hours.
>
> More confusing perhaps, but definitely shorter.
> As I said above, use it if you want or ignore it if it's too confusing.
>
> -=- Larry -=-
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
Beat me to the punch. I was about to suggest something similar, but I
thought that since one using this class would probably be "tick()-ing" more
than "str()-ing", It might also be better to store the value in seconds,
and convert to HH:MM:SS upon stringification; again using divmod:
class Clock(object):
def __init__(self,hours=0,minutes=0,seconds=0):
self.set(hours,minutes,seconds)
def tick(self):
self.__secs+=1
def set(self,hours, minutes, seconds):
self.__secs = seconds + (minutes*60) + (hours*60*60)
def __str__(self):
rest, seconds = divmod(self.__secs, 60)
hours, minutes = divmod(rest, 60)
return "%02d:%02d:%02d" % (hours, minutes, seconds)
def display(self):
print(str(self))
if __name__ == "__main__":
x = Clock()
print("Default construction, no parameters, Before ticks: {}".format(x))
for i in range(10000):
x.tick()
print("After ticks: {}".format(x))
x = Clock(hours=2, minutes=20, seconds=5)
print("\nConstructor with hours=2, minutes=20, seconds=5: {}".format(x))
print("Test of display() method: ",end=' ')
x.display()
This is my first post here, and I am a Python n00b (coming from that
four-letter word language "p***"), so if I broke some top/bottom posting or
formatting rules, I apologize, and would appreciate any posting pointers
Nathan H.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web