[Tutor] dictionary swittching

Raymond Hettinger python@rcn.com
Tue, 14 May 2002 07:22:25 -0400


Try changing all the 'elif' to 'if'.

Raymond Hettinger

----- Original Message -----
From: "Sharriff Aina" <nhytro-python@web.de>
To: <tutor@python.org>
Sent: Tuesday, May 14, 2002 7:17 AM
Subject: Re: [Tutor] dictionary swittching


Thanks for the replies, unfortunately none of the suggestions work. Actually
the snippet is for a CGI script that takes values from a form. I would like
different actions to be taken depending on the values of the keys:

##snippet


## below is used to convert the form output to a user friendlier dictionary
## code thanks to the python list

def FieldStorage2Dict(form):
    d = {}
    def decode_field(field):
      if isinstance(field, type([])):
        return map(decode_field, field)
      elif hasattr(field, "file") and field.file:
        return (field.filename, field.file)
      else:
        return field.value
    for key in form.keys():
      d[key] = decode_field(form[key])
    return d

formdict = FieldStorage2Dict(form)
#selector = ['agentnum','bnumber', 'tname', 'tvorname', 'tnumber',
'vnumber']
# these are the name values in the html form here just for clarity


if formdict.has_key('agentnum'):
  print 'test1'
elif formdict.has_key('bnumber'):
  print 'test2'
elif formdict.has_key('tname'):
  print 'test3'

##end snippet


sadly, only the first "if" works, I´m running Python 2.1.1 on a Wintel
machine

thanks
________________________________________________________________
Keine verlorenen Lotto-Quittungen, keine vergessenen Gewinne mehr!
Beim WEB.DE Lottoservice: http://tippen2.web.de/?x=13




_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor