Re: [Tutor] While loop counter

2008-07-07 Thread David

Thank you John & Allen,

See the loops section of my tutorial for more about for
and while loops.

Yes, great tutorial, just getting it to sink in, now thats the problem :)

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While loop counter

2008-07-07 Thread Alan Gauld

"David" <[EMAIL PROTECTED]> wrote

Hi, I am trying to get a while loop that will execute 5 time then 
stop.


Copngratulations, you have succeeded.
Unfortunately nothing happens inside your loop. I suspect you
actually want to execute some code 5 times?


#counter = 0
#while counter < 5 :
#counter = counter + 1


Yep, this would have done it.


while True:
   mytime.tick()
   mytime.print_time()
   time.sleep(1) # Sleep for 1 second


And this loop runs forever because you never break
out of it.


counter = 0
while counter < 5 :
   counter = counter + 1


If you did get out of the infinite loop above then  this one does do 
it too.
But all it does is increment the counter. If you want code to be 
executed

it must also be inside the loop (ie inside the indented code section).

So what I think you wanted would be:

counter = 0
while counter < 5:
   mytime.tick()
   mytime.print_time()
   time.sleep(1) # Sleep for 1 second
   counter = counter + 1

But since its for a fixed number of iterations you are better
off using a for loop:

for counter in range(5)
   mytime.tick()
   mytime.print_time()
   time.sleep(1) # Sleep for 1 second

Note we don't need to increment the counter in this version.

See the loops section of my tutorial for more about for
and while loops.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] While loop counter

2008-07-07 Thread John Fouhy
On 08/07/2008, David <[EMAIL PROTECTED]> wrote:
> Hi, I am trying to get a while loop that will execute 5 time then stop.

Hi David,

The standard pattern is like this:

i = 0
while i < 5:
# the stuff you want to do goes here
i = i + 1

Note that if you know exactly how many times you will execute your
loop, you should use a for loop instead:

for i in range(5):
# the stuff you want to do goes here

HTH!

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] While loop counter

2008-07-07 Thread David

Hi, I am trying to get a while loop that will execute 5 time then stop.

#!/usr/bin/python
# Filename myfirstclass.py
#
# A Little digital clock

from time_class import Time
import sys
import time

mytime = Time()

print "Can you tell me the time (24h)?"
hour = input("Give the hour: ")
minute = input("Give the minutes: ")
second = input("Give the seconds: ")

mytime.set_time(hour, minute, second)
#counter = 0
#while counter < 5 :
#counter = counter + 1
while True:
   mytime.tick()
   mytime.print_time()
   time.sleep(1) # Sleep for 1 second
counter = 0
while counter < 5 :
   counter = counter + 1

Here is the class;

#!/usr/bin/python
# Filename: time_class.py

class Time:

   def __init__(self):
   self.__hour = 0
   self.__minute = 0
   self.__second = 0

   def set_time(self, hour, minute, second):
   self.set_hour(hour)
   self.set_minute(minute)
   self.set_second(second)

   def set_hour(self, hour):
   if 0 <= hour and hour < 24:
   self.__hour = hour

   def set_minute(self, minute):
   if 0 <= minute and minute < 60:
 self.__minute = minute
  
   def set_second(self, second):

   if 0 <= second and second < 60:
   self.__second = second

   def tick(self):
   self.__second += 1
   self.__minute += self.__second / 60
   self.__hour += self.__minute / 60
  
   self.__hour = self.__hour % 24

   self.__minute = self.__minute % 60
   self.__second = self.__second % 60
  
  
   def print_time(self):

   print '%02d:%02d:%02d' % (self.__hour, self.__minute, self.__second)

Thanks, very new to Python.
-david

--
Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor