Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2288 > unrolled thread

Re: Learn Python the Hardway exercise 11 question 4

Started by"eryksun ()" <eryksun@gmail.com>
First post2011-03-31 09:12 -0700
Last post2011-04-01 07:35 +1100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Re: Learn Python the Hardway exercise 11 question 4 "eryksun ()" <eryksun@gmail.com> - 2011-03-31 09:12 -0700
    Re: Learn Python the Hardway exercise 11 question 4 Chris Angelico <rosuav@gmail.com> - 2011-04-01 03:18 +1100
    Re: Learn Python the Hardway exercise 11 question 4 Chris Angelico <rosuav@gmail.com> - 2011-04-01 07:35 +1100

#2288 — Re: Learn Python the Hardway exercise 11 question 4

From"eryksun ()" <eryksun@gmail.com>
Date2011-03-31 09:12 -0700
SubjectRe: Learn Python the Hardway exercise 11 question 4
Message-ID<54dde273-0528-4895-97d3-5ed77a0464bb@glegroupsg2000goo.googlegroups.com>
On Wednesday, March 30, 2011 11:03:09 PM UTC-4, JosephS 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?

There appears to be a formatting error here. The following works:

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)

Regarding the escape sequence, your're using %r which shows the representation (repr) of the string. For example:

In [1]: [x for x in height]
Out[1]: ['6', "'", '2', '"']

In [2]: print height
6'2"

In [3]: [x for x in repr(height)]
Out[3]: ["'", '6', '\\', "'", '2', '"', "'"]

In [4]: print repr(height)
'6\'2"'

The characters stored in height are just 6'2", but you couldn't input the string like that as a variable since Python uses quotes to demarcate a string literal. The backslash escapes the quote so that the interpreter will treat it as a regular character. It's unnecessary to escape the double quote, on the other hand, since this string literal is marked with single quotes. If the string were instead inputted with double quotes, then the final double quote would have to be escaped, such as the following: height = "6'2\"". Finally, notice that in the output representation of the characters of repr(height) (Out[3]) that the backslash itself has to be escaped. Otherwise it would be interpreted as escaping the single quote that follows.

[toc] | [next] | [standalone]


#2289

FromChris Angelico <rosuav@gmail.com>
Date2011-04-01 03:18 +1100
Message-ID<mailman.36.1301588333.2990.python-list@python.org>
In reply to#2288
On Fri, Apr 1, 2011 at 3:12 AM, eryksun () <eryksun@gmail.com> 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!

[toc] | [prev] | [next] | [standalone]


#2300

FromChris Angelico <rosuav@gmail.com>
Date2011-04-01 07:35 +1100
Message-ID<mailman.42.1301603750.2990.python-list@python.org>
In reply to#2288
On Fri, Apr 1, 2011 at 4:54 AM, Dan Stromberg <drsalists@gmail.com> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web