473,657 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

advice on this little script

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.Collecti ons.Generic;
using System.Text;
using System.Threadin g;

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

for (int i = iTime - 1; i > 0; i--)
{
Console.WriteLi ne("{0} minute(s) remaining.", i);
Thread.Sleep(60 000);
}

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

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

8 lines vs. 31 lines! :)
Mar 9 '06 #1
32 1847
John Salerno wrote:
sleep(1.0)


Heh heh, that was for testing. Obviously it should read 60.0 (is a float
necessary at all?).
Mar 9 '06 #2
John Salerno wrote:
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.'
---------------------


Very nice, but maybe

....
sleep(60.0)

This corrects for the number of seconds in a minute.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Mar 9 '06 #3
What about a console beep? How do you add that?

rpd

Mar 9 '06 #4
On 2006-03-09, John Salerno <jo******@NOSPA Mgmail.com> wrote:
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.'


for x in range(minutes,0 ,-1):
sleep(60.0)
print minutes, 'minutes remaining'

--
Grant Edwards grante Yow! My EARS are GONE!!
at
visi.com
Mar 9 '06 #5
James Stroud wrote:
Very nice, but maybe

...
sleep(60.0)

This corrects for the number of seconds in a minute.

James


Thanks! And yeah, I fixed that little issue. If only laundry could be
done that fast. :)
Mar 9 '06 #6
BartlebyScriven er wrote:
What about a console beep? How do you add that?

rpd


Ooh, good point! I forgot about the most important part, otherwise I'd
never know it was done and someone would steal my clothes! :)

Time to do some library reference research....
Mar 9 '06 #7
Grant Edwards wrote:
On 2006-03-09, John Salerno <jo******@NOSPA Mgmail.com> wrote:
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.'


for x in range(minutes,0 ,-1):
sleep(60.0)
print minutes, 'minutes remaining'


Nice! Cross off another line! I feel like Hemingway. :)
Mar 9 '06 #8
John Salerno wrote:
from time import sleep .... sleep(1.0)


Very picky point, but I'd like to know what others think of this. Should
I import as above, or should I do this:

import time
.....
time.sleep(60.0 ) ???

I think the 'from time import sleep' looks cleaner, because I'm only
taking what I need (is an import any more expensive than this from?),
but I also feel like the 'time.sleep' syntax is much more
self-describing and better to read than just 'sleep'.

So what do you guys think between these two choices?
Mar 9 '06 #9
BartlebyScriven er wrote:
What about a console beep? How do you add that?

rpd


Just use ASCII code 007 (BEL/BEEP):
import sys
sys.stdout.writ e('\007')


Or if you're on Windows, use the winsound standard module.

--Ben

Mar 9 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1290
by: rebeccatre | last post by:
hi, i am still figuring out joins and whatnot, could someone help me achieve this goal? <script> var setnumber = 5; var total5 = 100; alert(total+setnumber); </script>
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.