Remember that short article that talked about sleeping time? I figured we should practice it! The only things we've been doing with time is giving the user some time to read a message before the next one comes up. There is way more to it than that, though! Here's another program I wrote a very long time ago. It's a timer (that figures, since the title of this article is "Timer").
Let's see the program:
import time, easygui
choices=easygui.buttonbox("Do you want the time to go in seconds, minutes, or hours?", choices=['Seconds', 'Minutes', 'Hours'])
if choices == 'Seconds':
time2=easygui.integerbox("Seconds. OK. How many seconds?")
for i in range(time2, 0, -1):
print(time2)
print(time2 * "*")
time.sleep(1)
time2=time2-1
elif choices == 'Minutes':
time3=easygui.integerbox("How many minutes?")
for j in range(time3, 0, -1):
print(time3)
print(time3 * "*")
time.sleep(1*60)
time3=time3-1
else:
time4=easygui.integerbox("Long times. How many hours?")
for k in range(time4, 0, -1):
print(time4)
print(time4 * "*")
time.sleep(1*60*60)
time4=time4-1
easygui.msgbox("Time's up!")
It may seem a little complicated, but let's just see what happens when it runs.
10
**********
9
*********
8
********
7
*******
6
******
5
*****
4
****
3
***
2
**
1
*
Not too complicated, really. The asterisks were mainly for fun, so let's just move on to the real numbers.
for i in range(time2, 0, -1):
We already know about this part, we talked about it in the For Loops article.
print(time2)
print(time2 * "*")
time.sleep(1)
time2=time2-1
Well, we print time2. And then we print as many asterisks as time2 is. Then we wait a second, and then we recreate the variable.
So, this is another part of the world of time. Now you can use this program to your heart's content! I challenge you to find out if time2=time2-1, time3=time3-1, and time4=time4-1 are pointless!
No comments:
Post a Comment