[Tutor] checking for expected types from input file

Bryan Fodness bryan.fodness at gmail.com
Thu Jul 31 03:43:07 CEST 2008


I am populating a dictionary from an input file, and would like to create an
error code if a string is sent to a variable that expects a float or int.

INPUT = {}
for line in open(infile):
    input_line = line.split(' = ')
    INPUT[input_line[0].strip()] = input_line[1].strip()

if INPUT.has_key('ReferencePositionX'):
    refx = float(INPUT['ReferencePositionX'])/10
    refy = float(INPUT['ReferencePositionY'])/10
    refz = float(INPUT['ReferencePositionZ'])/10

I have many variables of different types, and I want to do a check in
case something like ReferencePositionX = abc occurs.

-- 
"The game of science can accurately be described as a never-ending insult to
human intelligence." - João Magueijo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080730/9caa5c50/attachment.htm>


More information about the Tutor mailing list