Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Learn Python the Hardway exercise 11 question 4 (http://www.velocityreviews.com/forums/t746063-learn-python-the-hardway-exercise-11-question-4-a.html)

Joseph Sanoyo 03-31-2011 03:03 AM

Learn Python the Hardway exercise 11 question 4
 
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 03-31-2011 07:53 AM

Re: Learn Python the Hardway exercise 11 question 4
 
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 03-31-2011 12:43 PM

Re: Learn Python the Hardway exercise 11 question 4
 
Joseph Sanoyo a écrit ce jeudi 31 mars 2011 05:03 dans <b2293547-6153-48e6-
a208-fca4e439974f@s9g2000yqm.googlegroups.com> :

>>> 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 03-31-2011 04:18 PM

Re: Learn Python the Hardway exercise 11 question 4
 
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!

Chris Angelico 03-31-2011 08:35 PM

Re: Learn Python the Hardway exercise 11 question 4
 
On Fri, Apr 1, 2011 at 4:54 AM, Dan Stromberg <drsalists@gmail.com> 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 03-31-2011 09:57 PM

Re: Learn Python the Hardway exercise 11 question 4
 
On Thu, Mar 31, 2011 at 2:43 PM, eryksun () <eryksun@gmail.com> 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 03-31-2011 10:14 PM

Re: Learn Python the Hardway exercise 11 question 4
 
On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <debatem1@gmail.com> 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 03-31-2011 11:08 PM

Re: Learn Python the Hardway exercise 11 question 4
 
Chris Angelico <rosuav@gmail.com> writes:

> On Fri, Apr 1, 2011 at 8:57 AM, geremy condra <debatem1@gmail.com> 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/


All times are GMT. The time now is 08:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.