[Tutor] changing string to integer in a script

python at kc.rr.com python at kc.rr.com
Sun Jun 20 19:29:28 EDT 2004


Hi, all. Having a problem with a script that I can't seem to lick. I 
*think* I know what the issue is, but I am powerless to fix it.

In short, it will attempt to see if myvar is located in list mylist. If 
it does, then something interesting happens. Otherwise, it will sleep 
for a bit and try again. (The script will ultimately be for logging 
someone out automagically during certain hours of the day). Ok, 
script...
-----
import sys, os, time, EasyDialogs

mylist = [ 9, 10, 16, 17, 18, 19, 20, 21]
# If i assign myvar to the below, it works peachy.
#myvar = 9
# If I assign myvar to the system time, it fails. always.
myvar = time.strftime('%H')
# All below this works fine. you can cheerfully ignore it.
try:
	mylist.index(myvar)
except ValueError:
	EasyDialogs.Message('No problems. Sleeping. blah blah...')
else:
	EasyDialogs.Message('something interesting happens here...')
-----
Ok. if I set the myvar to time.strftime('%H') it will always fail, even 
when I try to set the clock back, et. al. I run a 'type' against it and 
get:

 >>> import time
 >>> time.strftime('%H')
'18'
 >>> type (time.strftime('%H'))
<type 'str'>

So it looks like I am getting a string back instead of an integer. If I 
just assign it a number and run type on it again it looks OK...

 >>> myvar = 9
 >>> type(myvar)
<type 'int'>

I'm sure I have not looked hard enough, but I can't see any way to 
change a string to an int. I *imagine* it would be something kinda 
like:

time.strftime('%H').something.something()

But I'm stuck and need an extra bit of cleverness to fix this. At the 
moment I am *assuming* the string .vs int. thing is the issue, would 
love to know if it's really something else I have not run into yet.
Sorry for the length, but would rather soak you with all I have than be 
inscrutable. Thanks in advance for any bits that may help out.

P.S. First newbie post! :D

Cheers!

tom

Weinberg's Law:

"If builders built buildings the way programmers wrote programs, then 
the first woodpecker that came along would destroy civilization"




More information about the Tutor mailing list