Python riddle

Discussion in 'Python' started by SPE - Stani's Python Editor, Dec 6, 2005.

  1. 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?'
    SPE - Stani's Python Editor, Dec 6, 2005
    #1
    1. Advertising

  2. SPE - Stani's Python Editor

    ech0 Guest

    1 is not greater then 2 last time i checked :)
    ech0, Dec 6, 2005
    #2
    1. Advertising

  3. Forget it, it's not 1st of april yet.
    SPE - Stani's Python Editor, Dec 6, 2005
    #3
  4. SPE - Stani's Python Editor wrote:
    > 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?'
    >


    because 1 is not greater than 2 I suppose :)
    =?ISO-8859-1?Q?Daniel_Sch=FCle?=, Dec 6, 2005
    #4
  5. SPE - Stani's Python Editor

    kyle.tk Guest

    SPE - Stani's Python Editor wrote:
    > 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?'


    last time i checked this should print 'why?'
    I have no idea how you got it to print 'Why?'
    kyle.tk, Dec 6, 2005
    #5
  6. SPE - Stani's Python Editor

    Dan Bishop Guest

    kyle.tk wrote:
    > SPE - Stani's Python Editor wrote:
    > > 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?'

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


    >>> class CapOutput:

    .... def __init__(self, fileobj):
    .... self._file = fileobj
    .... def write(self, text):
    .... self._file.write(text.capitalize())
    ....
    >>> sys.stdout = CapOutput(sys.stdout)
    >>> print 'why?'

    Why?
    Dan Bishop, Dec 6, 2005
    #6
  7. On Mon, 05 Dec 2005 22:58:42 -0800, Dan Bishop wrote:

    >>>> class CapOutput:

    > ... def __init__(self, fileobj):
    > ... self._file = fileobj
    > ... def write(self, text):
    > ... self._file.write(text.capitalize())
    > ...
    >>>> sys.stdout = CapOutput(sys.stdout)
    >>>> print 'why?'

    > Why?



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

    But mostly evil.


    *grin*


    --
    Steven.
    Steven D'Aprano, Dec 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HK
    Replies:
    0
    Views:
    282
  2. Juha Haataja

    Solving the Einstein's Riddle in Python

    Juha Haataja, May 24, 2004, in forum: Python
    Replies:
    10
    Views:
    886
    Christos TZOTZIOY Georgiou
    May 25, 2004
  3. pythonchallenge
    Replies:
    76
    Views:
    1,557
    Tim Peters
    May 27, 2005
  4. pythonchallenge
    Replies:
    0
    Views:
    223
    pythonchallenge
    May 8, 2005
  5. Sara Khalatbari
    Replies:
    1
    Views:
    284
    Peter Hansen
    May 10, 2005
Loading...

Share This Page