Learn Python the Hardway exercise 11 question 4

Discussion in 'Python' started by Joseph Sanoyo, Mar 31, 2011.

  1. print "How old are you?", age = raw_input()
    print "How tall are you?", height = raw_input()
    print "How much do you weigh?", weight = raw_input()
    print "So, you're %r old, %r tall and %r heavy." % ( age, height,
    weight)
    Note:
    Notice that we put a , (comma) at the end of each print line. This is
    so that print doesn’t end the line with a newline and go to the next
    line.
    What You Should See
    Extra Credit
    1. Go online and find out what Python’s raw_input does.
    $ python ex11.py How old are you?
    35 How tall are you?
    6'2" How much do you weigh? 180lbs
    So, you're '35' old, '6\'2"' tall and '180lbs' heavy.

    Related to escape sequences, try to find out why the last line has
    ’6\’2"’ with that \’ sequence. See how the single-quote needs to be
    escaped because otherwise it would end the string?
    Joseph Sanoyo, Mar 31, 2011
    #1
    1. Advertising

  2. On Wed, 30 Mar 2011 20:03:09 -0700, Joseph Sanoyo wrote:

    > print "How old are you?", age = raw_input() print "How tall are you?",
    > height = raw_input() print "How much do you weigh?", weight =
    > raw_input() print "So, you're %r old, %r tall and %r heavy." % ( age,
    > height, weight)
    > Note:
    > Notice that we put a , (comma) at the end of each print line. This is so
    > that print doesn’t end the line with a newline and go to the next line.
    > What You Should See
    > Extra Credit
    > 1. Go online and find out what Python’s raw_input does. $ python ex11.py
    > How old are you?
    > 35 How tall are you?
    > 6'2" How much do you weigh? 180lbs
    > So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
    >
    > Related to escape sequences, try to find out why the last line has
    > ’6\’2"’ with that \’ sequence. See how the single-quote needs to be
    > escaped because otherwise it would end the string?


    I'm sorry, are you asking a question and expecting an answer, replying to
    somebody else's question, or just sharing something you thought was
    interesting?


    --
    Steven
    Steven D'Aprano, Mar 31, 2011
    #2
    1. Advertising

  3. Joseph Sanoyo

    Web Dreamer Guest

    Joseph Sanoyo a écrit ce jeudi 31 mars 2011 05:03 dans <b2293547-6153-48e6-
    > :

    >>> print "How old are you?", age = raw_input()

    File "<stdin>", line 1
    print "How old are you?", age = raw_input()
    ^
    SyntaxError: invalid syntax

    >>> print "How tall are you?", height = raw_input()

    File "<stdin>", line 1
    print "How tall are you?", height = raw_input()
    ^
    SyntaxError: invalid syntax

    >>> print "How much do you weigh?", weight = raw_input()

    File "<stdin>", line 1
    print "How much do you weigh?", weight = raw_input()
    ^
    SyntaxError: invalid syntax

    >>> print "So, you're %r old, %r tall and %r heavy." % ( age, height,

    weight)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'height' is not defined

    Normal, since the above lines failed, age, height and weight are not defined


    > What You Should See

    The exact errors above.

    > Extra Credit
    > 1. Go online and find out what Python?s raw_input does.
    > $ python ex11.py How old are you?

    File "ex11.py", line 1
    print "How old are you?", age = raw_input()
    ^
    SyntaxError: invalid syntax

    > 35 How tall are you?
    > 6'2" How much do you weigh? 180lbs
    > So, you're '35' old, '6\'2"' tall and '180lbs' heavy.
    >
    > Related to escape sequences, try to find out why the last line has
    > ?6\?2"? with that \? sequence. See how the single-quote needs to be
    > escaped because otherwise it would end the string?


    First all the above code needs to be fixed and you should write:
    age = raw_input("How old are you? ")
    height = raw_input("How tall are you? ")
    weight = raw_input("How much do you weigh? " )
    print "So, you're %r old, %r tall and %r heavy." % ( age, height, weight)

    then:
    $ python ex11.py
    How old are you? 10
    How tall are you? 10
    How much do you weigh? 10
    So, you're '10' old, '10' tall and '10' heavy.

    If you replace %r by %s in the print you get:
    So, you're 10 old, 10 tall and 10 heavy.

    --
    Web Dreamer
    Web Dreamer, Mar 31, 2011
    #3
  4. On Fri, Apr 1, 2011 at 3:12 AM, eryksun () <> wrote:
    > There appears to be a formatting error here.


    So remind me again why Python likes whitespace to be significant?

    </troll>

    :)

    Chris Angelico
    PS. Yes, I know "remind me again" is redundant. You have to make
    mistakes when you troll, it's a moral imperative!
    Chris Angelico, Mar 31, 2011
    #4
  5. On Fri, Apr 1, 2011 at 4:54 AM, Dan Stromberg <> wrote:
    >
    > http://stromberg.dnsalias.org/~strombrg/significant-whitespace.html
    >


    I was trolling, I know the reasons behind it. Anyway, most people
    don't share code by email! (Actually, since you seem to be the author
    of that page - could you address that particular point? I think it's
    probably as big an issue as any of the others, to today's coders -
    "code semantics get destroyed by forums/email/etc/etc/etc".)

    Solution: All emailed code should begin with
    from __future__ import braces
    And there you are, out of your difficulty at once!

    Chris Angelico
    tongue still firmly stuck in cheek
    Chris Angelico, Mar 31, 2011
    #5
  6. On Thu, Mar 31, 2011 at 2:43 PM, eryksun () <> wrote:
    > On Thursday, March 31, 2011 4:35:42 PM UTC-4, Chris Angelico wrote:
    >>
    >> I was trolling, I know the reasons behind it. Anyway, most people
    >> don't share code by email! (Actually, since you seem to be the author
    >> of that page - could you address that particular point? I think it's
    >> probably as big an issue as any of the others, to today's coders -
    >> "code semantics get destroyed by forums/email/etc/etc/etc".)
    >>
    >> Solution: All emailed code should begin with
    >> from __future__ import braces
    >> And there you are, out of your difficulty at once!

    >
    > You could paste it as a base64 stream, such as:
    >
    >
    >> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
    >> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI=

    >
    >
    > Then decode and exec:
    >
    > In [1]: import base64
    >
    > In [2]: %cpaste
    > Pasting code; enter '--' alone on the line to stop.
    > :code="""> ZGVmIHNwYW0oKToNCiAgICBwcmludCAiU3BhbSEg
    > :> TG92ZWx5IHNwYW0hIExvdmVseSBzcGFtISI="""
    > :--
    >
    > In [3]: print base64.b64decode(code)
    > def spam():
    >    print "Spam! Lovely spam! Lovely spam!"
    >
    > In [4]: exec(base64.b64decode(code))
    >
    > In [5]: spam()
    > Spam! Lovely spam! Lovely spam!


    I know it's tongue-in-cheek, but please, please, please don't do this.

    Geremy Condra
    geremy condra, Mar 31, 2011
    #6
  7. On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <> wrote:
    > I know it's tongue-in-cheek, but please, please, please don't do this.


    It would be more secure to base64 it and then rot13 the output.

    Chris Angelico
    /me is feeling evil today

    === Begin Base-Rotten 64-13 ===
    FKDtq291oTDtLzHtoJ9lMFOmMJA1pzHtqT8tLzSmMGL0VTy0VTShMPO0nTIhVUWiqQRmVUEbMFOi
    qKEjqKDhQDbAPxAbpzymVRShM2IfnJAiQDbioJHtnKZtMzIyoTyhMlOyqzyfVUEiMTS5QDb=
    === End Base-Rotten 64-13 ===
    Chris Angelico, Mar 31, 2011
    #7
  8. Joseph Sanoyo

    John Bokma Guest

    Chris Angelico <> writes:

    > On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <> wrote:
    >> I know it's tongue-in-cheek, but please, please, please don't do this.

    >
    > It would be more secure to base64 it and then rot13 the output.


    Rot-13 twice, to make it even more secure ;-)


    --
    John Bokma j3b

    Blog: http://johnbokma.com/ Facebook: http://www.facebook.com/j.j.j.bokma
    Freelance Perl & Python Development: http://castleamber.com/
    John Bokma, Apr 1, 2011
    #8
    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. Porky Pig Jr
    Replies:
    3
    Views:
    1,017
    Fuzzyman
    May 12, 2004
  2. CoreyWhite
    Replies:
    1
    Views:
    641
    JohnQ
    Mar 31, 2007
  3. Alexander
    Replies:
    20
    Views:
    1,027
    BGB / cr88192
    Sep 11, 2010
  4. neil harper
    Replies:
    1
    Views:
    274
    Raymond Hettinger
    Mar 31, 2011
  5. eryksun ()
    Replies:
    0
    Views:
    663
    eryksun ()
    Mar 31, 2011
Loading...

Share This Page