advice on this little script

John Salerno johnjsal at NOSPAMgmail.com
Thu Mar 9 05:06:40 CET 2006


My first project when I started learning C# was to make a little timer 
to tell me when my laundry was done :) and I thought it would be fun to 
convert this to Python. Here's what I came up with after much struggling 
with the Timer class from the threading module -- as you can see, I 
abandoned it for the sleep() function from timer.

Please let me know if this is a good (i.e. Pythonic) way of doing this, 
and if it can be improved (although I don't need anything fancier than 
this basic functionality).
---------------------
from time import sleep

minutes = input('Enter the number of minutes to wait: ')

for x in range(minutes):
	sleep(1.0)
	minutes -= 1
	print minutes, 'minutes remaining.'
---------------------

And if you are interested, here is the original C# code in all its 
monstrous glory. I can't believe the difference!
----------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace MyTimer
{
     class Timer
     {
         static void Main()
         {
             Console.Write("Enter the number of minutes to wait: ");
             int iTime = Int32.Parse(Console.ReadLine());
             Thread.Sleep(60000);

             for (int i = iTime - 1; i > 0; i--)
             {
                 Console.WriteLine("{0} minute(s) remaining.", i);
                 Thread.Sleep(60000);
             }

             Console.Write("{0} minutes have elapsed.", iTime);

             for (int i = 3; i > 0; i--)
             {
                 Console.Beep();
                 Thread.Sleep(1000);
             }
         }
     }
}
--------------------------

8 lines vs. 31 lines!  :)



More information about the Python-list mailing list