Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #98693

What is wrong in this example code?

Newsgroups comp.lang.python
Date 2015-11-12 05:57 -0800
Message-ID <1b204966-46c6-4890-a5a5-8addd20343a1@googlegroups.com> (permalink)
Subject What is wrong in this example code?
From fl <rxjwg98@gmail.com>

Show all headers | View raw


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)

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


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