Velocity Reviews > Learn Python the Hardway exercise 11 question 4

# Learn Python the Hardway exercise 11 question 4

Joseph Sanoyo
Guest
Posts: n/a

 03-31-2011
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?

Steven D'Aprano
Guest
Posts: n/a

 03-31-2011
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

Web Dreamer
Guest
Posts: n/a

 03-31-2011
Joseph Sanoyo a écrit ce jeudi 31 mars 2011 05:03 dans <b2293547-6153-48e6-
http://www.velocityreviews.com/forums/(E-Mail Removed)> :

>>> 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

Chris Angelico
Guest
Posts: n/a

 03-31-2011
On Fri, Apr 1, 2011 at 3:12 AM, eryksun () <(E-Mail Removed)> 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
Guest
Posts: n/a

 03-31-2011
On Fri, Apr 1, 2011 at 4:54 AM, Dan Stromberg <(E-Mail Removed)> wrote:
>
> http://stromberg.dnsalias.org/~strom...hitespace.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

geremy condra
Guest
Posts: n/a

 03-31-2011
On Thu, Mar 31, 2011 at 2:43 PM, eryksun () <(E-Mail Removed)> 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

Chris Angelico
Guest
Posts: n/a

 03-31-2011
On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <(E-Mail Removed)> 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 ===
FKDtq291oTDtLzHtoJ9lMFOmMJA1pzHtqT8tLzSmMGL0VTy0VT ShMPO0nTIhVUWiqQRmVUEbMFOi
qKEjqKDhQDbAPxAbpzymVRShM2IfnJAiQDbioJHtnKZtMzIyoT yhMlOyqzyfVUEiMTS5QDb=
=== End Base-Rotten 64-13 ===

John Bokma
Guest
Posts: n/a

 03-31-2011
Chris Angelico <(E-Mail Removed)> writes:

> On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <(E-Mail Removed)> 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

Freelance Perl & Python Development: http://castleamber.com/