try / except not worknig correctly

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • '@'.join([..join(['fred','dixon']),..join(['gmail'

    #1

    try / except not worknig correctly

    the code below will not execute the except section when i enter a
    number.
    what am i missing ?

    ############### ############### ###########
    ..while 1:
    .. print 'Pump Protection ? '
    .. #line 133
    .. try:
    .. myInput = raw_input('A B C D E F G H I J K L ? ')
    .. myInput = string.upper(my Input)
    .. if myInput == 'A':break #
    .. if myInput == 'B':break #
    .. if myInput == 'C':break #
    .. if myInput == 'D':break #
    .. if myInput == 'E':break #
    .. if myInput == 'F':break #
    .. if myInput == 'G':break #
    .. if myInput == 'H':break #
    .. if myInput == 'I':break #
    .. if myInput == 'J':break #
    .. if myInput == 'K':break #
    .. if myInput == 'L':break #
    .. except ValueError:
    .. print "Oops! That was no valid number. Try again..."
    .. pass


    ..'s are to preserve indents.
    fred

  • Robert Kern

    #2
    Re: try / except not worknig correctly

    '@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:[color=blue]
    > the code below will not execute the except section when i enter a
    > number.
    > what am i missing ?[/color]

    Something that will actually raise ValueError.

    It looks like you could use the help of the Python Tutor mailing list.


    [color=blue]
    > ############### ############### ###########
    > .while 1:
    > . print 'Pump Protection ? '
    > . #line 133
    > . try:
    > . myInput = raw_input('A B C D E F G H I J K L ? ')
    > . myInput = string.upper(my Input)
    > . if myInput == 'A':break #
    > . if myInput == 'B':break #
    > . if myInput == 'C':break #
    > . if myInput == 'D':break #
    > . if myInput == 'E':break #
    > . if myInput == 'F':break #
    > . if myInput == 'G':break #
    > . if myInput == 'H':break #
    > . if myInput == 'I':break #
    > . if myInput == 'J':break #
    > . if myInput == 'K':break #
    > . if myInput == 'L':break #
    > . except ValueError:
    > . print "Oops! That was no valid number. Try again..."
    > . pass
    >
    >
    > .'s are to preserve indents.
    > fred[/color]

    --
    Robert Kern
    rkern@ucsd.edu

    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter

    Comment

    • Brian van den Broek

      #3
      Re: try / except not worknig correctly

      '@'.join([..join(['fred', 'dixon']), ..join(['gmail', 'com'])]) said
      unto the world upon 2005-03-12 19:20:[color=blue]
      > the code below will not execute the except section when i enter a
      > number.
      > what am i missing ?
      >
      > ############### ############### ###########
      > .while 1:
      > . print 'Pump Protection ? '
      > . #line 133
      > . try:
      > . myInput = raw_input('A B C D E F G H I J K L ? ')
      > . myInput = string.upper(my Input)
      > . if myInput == 'A':break #
      > . if myInput == 'B':break #
      > . if myInput == 'C':break #
      > . if myInput == 'D':break #
      > . if myInput == 'E':break #
      > . if myInput == 'F':break #
      > . if myInput == 'G':break #
      > . if myInput == 'H':break #
      > . if myInput == 'I':break #
      > . if myInput == 'J':break #
      > . if myInput == 'K':break #
      > . if myInput == 'L':break #
      > . except ValueError:
      > . print "Oops! That was no valid number. Try again..."
      > . pass
      >
      >
      > .'s are to preserve indents.
      > fred
      >[/color]

      Hi fred,

      You are missing that raw_input returns a string.

      PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)]
      on win32.
      Portions Copyright 1994-2004 Mark Hammond (mhammond@skipp inet.com.au)
      - see 'Help/About PythonWin' for further copyright information.[color=blue][color=green][color=darkred]
      >>> example = raw_input('Give me a number!')[/color][/color][/color]
      Give me a number!42[color=blue][color=green][color=darkred]
      >>> type(example)[/color][/color][/color]
      <type 'str'>[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      I am also not sure why you expected a ValueError:
      [color=blue][color=green][color=darkred]
      >>> import string
      >>> a = 42
      >>> string.upper(a)[/color][/color][/color]
      Traceback (most recent call last):
      File "<interacti ve input>", line 1, in ?
      File "C:\PYTHON24\li b\string.py", line 235, in upper
      return s.upper()
      AttributeError: 'int' object has no attribute 'upper'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]


      Additionally, string methods are often preferred to the string module:[color=blue][color=green][color=darkred]
      >>> b = 'lower'
      >>> b.upper()[/color][/color][/color]
      'LOWER'[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]


      You might also want to test

      if myInput in ('A', 'B', 'C', 'etc'):
      .. break

      instead of your chain of if tests.

      Best,

      Brian vdB


      Comment

      • '@'.join([..join(['fred','dixon']),..join(['gmail'

        #4
        Re: try / except not worknig correctly

        1) the tutor list is really slow. but thanks.

        2) Thanks Brain, i was missing the string bit. the code i posted (opps)
        was not exactly where i was having problems, it just looked like it.

        also thanks for the 'in' test, that will come in handy.
        i am using chain because i need to do something different for each
        twest.

        is there a better way to return a tuple for each choice ?

        Comment

        • Pete

          #5
          Re: try / except not worknig correctly

          '@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:[color=blue]
          > 1) the tutor list is really slow. but thanks.
          >
          > 2) Thanks Brain, i was missing the string bit. the code i posted (opps)
          > was not exactly where i was having problems, it just looked like it.
          >
          > also thanks for the 'in' test, that will come in handy.
          > i am using chain because i need to do something different for each
          > twest.
          >
          > is there a better way to return a tuple for each choice ?
          >[/color]
          You could try this:[color=blue][color=green][color=darkred]
          >>> def doA():[/color][/color][/color]
          print 'doing A'
          [color=blue][color=green][color=darkred]
          >>> def doB():[/color][/color][/color]
          print 'doing B'
          [color=blue][color=green][color=darkred]
          >>> processFuncs = dict()
          >>> processFuncs['A'] = doA
          >>> processFuncs['B'] = doB
          >>> while 1:[/color][/color][/color]
          print 'Enter option:'
          try:
          myInput = raw_input('A or B?')
          myInput = myInput.upper()

          func = processFuncs[myInput]
          func()
          except KeyError, e:
          print 'Opps wrong option', e
          pass


          Enter option:
          A or B?A
          doing A
          Enter option:
          A or B?B
          doing B
          Enter option:
          A or B?C
          Opps wrong option 'C'
          Enter option:
          A or B?

          Comment

          • Jeremy Bowers

            #6
            Re: try / except not worknig correctly

            On Sat, 12 Mar 2005 17:06:17 -0800,
            '@'.join([..join(['fred','dixon']),..join(['gmail','com'])]) wrote:

            I'd also suggest

            validInput = "ABCDEFGHIJ KL" # and there are more clever ways to do this,
            # but this will do

            myInput = raw_input(" ".join(validInp ut) + "?")
            if len(myInput) != 1:
            # do whatever
            pass
            if myInput not in validInput:
            raise ValueError("Inp ut not in legal input: " + validInput)


            Obviously not a drop-in replacement for your code, but ought to clean it
            up a little.

            Comment

            Working...