Python riddle

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SPE - Stani's Python Editor

    #1

    Python riddle

    I know that this code is nonsense, but why does this print 'Why?'

    a = 1
    if a >2:
    try:
    5/0
    except:
    raise
    else:
    print 'why?'

  • ech0

    #2
    Re: Python riddle

    1 is not greater then 2 last time i checked :)

    Comment

    • SPE - Stani's Python Editor

      #3
      Re: Python riddle

      Forget it, it's not 1st of april yet.

      Comment

      • Daniel Schüle

        #4
        Re: Python riddle

        SPE - Stani's Python Editor wrote:[color=blue]
        > I know that this code is nonsense, but why does this print 'Why?'
        >
        > a = 1
        > if a >2:
        > try:
        > 5/0
        > except:
        > raise
        > else:
        > print 'why?'
        >[/color]

        because 1 is not greater than 2 I suppose :)

        Comment

        • kyle.tk

          #5
          Re: Python riddle


          SPE - Stani's Python Editor wrote:[color=blue]
          > I know that this code is nonsense, but why does this print 'Why?'
          >
          > a = 1
          > if a >2:
          > try:
          > 5/0
          > except:
          > raise
          > else:
          > print 'why?'[/color]

          last time i checked this should print 'why?'
          I have no idea how you got it to print 'Why?'

          Comment

          • Dan Bishop

            #6
            Re: Python riddle

            kyle.tk wrote:[color=blue]
            > SPE - Stani's Python Editor wrote:[color=green]
            > > I know that this code is nonsense, but why does this print 'Why?'
            > >
            > > a = 1
            > > if a >2:
            > > try:
            > > 5/0
            > > except:
            > > raise
            > > else:
            > > print 'why?'[/color]
            >
            > last time i checked this should print 'why?'
            > I have no idea how you got it to print 'Why?'[/color]
            [color=blue][color=green][color=darkred]
            >>> class CapOutput:[/color][/color][/color]
            .... def __init__(self, fileobj):
            .... self._file = fileobj
            .... def write(self, text):
            .... self._file.writ e(text.capitali ze())
            ....[color=blue][color=green][color=darkred]
            >>> sys.stdout = CapOutput(sys.s tdout)
            >>> print 'why?'[/color][/color][/color]
            Why?

            Comment

            • Steven D'Aprano

              #7
              Re: Python riddle

              On Mon, 05 Dec 2005 22:58:42 -0800, Dan Bishop wrote:
              [color=blue][color=green][color=darkred]
              >>>> class CapOutput:[/color][/color]
              > ... def __init__(self, fileobj):
              > ... self._file = fileobj
              > ... def write(self, text):
              > ... self._file.writ e(text.capitali ze())
              > ...[color=green][color=darkred]
              >>>> sys.stdout = CapOutput(sys.s tdout)
              >>>> print 'why?'[/color][/color]
              > Why?[/color]


              That's ... brilliant. Brilliant and evil.

              But mostly evil.


              *grin*


              --
              Steven.

              Comment

              Working...