srtring to int question
Darrell Gallion
darrell at dorb.com
Sat Aug 12 22:38:31 EDT 2000
Take a look at DateTime in the zope distribution or
http://starship.python.net/~lemburg/mxDateTime.html
------------------------------
def convert(s):
try:
x=eval(s,{},{})
except:
print "Not a number"
return None
return x
while 1:
s=raw_input("Enter a number:")
print convert(s)
--------------------------------------
import re, string
def convert(s):
try:
assert(re.match("\s*\d+",s))
x = int(s)
except:
print "Not a number"
return None
return x
while 1:
s=raw_input("Enter a number:")
print convert(s)
-------------------------------------
Enter a number:3
3
Enter a number:import sys
Not a number
None
Enter a number:dir()
[]
Enter a number:
--Darrell
----- Original Message -----
From: "Jim Richardson"
> For various reasons, I have written a small bit of python code to
> take a 1-3 julian day, and return the day-month form. It works
> ok mostly (available at
http://www.eskimo.com/~warlock/freesoft/julian_py.tgz)
> OK, the question, I use raw_input to poll for the julian day, and
> then convert to int via int, but how do I check that the value from
raw_input
> is "intable"? that is, it works fine if I pass a number, but if I type in
> non-numerical characters, [a-zA-Z!@# etc] then the code errors on the
> int() function. Any pointers? thanks
>
More information about the Python-list
mailing list